求助!84分
查看原帖
求助!84分
528430
FiraCode楼主2022/5/12 20:13
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <set>
#include <map>
using namespace std;
const int N = 1000002;
int n, m;
int ne[N];
int tn, tm;
string p, s;
string a[N], b[N];
int ans1 = 0;
int main() {
	string temp;
	while (cin >> temp, temp != "$") {
		a[++ tn] = temp;
	}
	while (cin >> temp, temp != "$") {
		b[++ tm] = temp;
	}
	char last;
	for (int i = 1; i <= tn; ++ i) {
		if (i == 1) {
			p += '1';
			last = '1';
			continue;
		}
		p += (a[i] == a[i - 1] ? last : (3 - (p[(int)p.size() - 1] - '0')) + '0');
		if (p[int(p.size() - 1)] != last) last = p[int(p.size() - 1)];
	}
	for (int i = 1; i <= tm; ++ i) {
		if (i == 1) {
			s += '1';
			last = '1';
			continue;
		}
//		cout << (s[(int)s.size() - 1] - '0') << ' ';
//		cout << ((3 - (s[(int)s.size() - 1] - '0')) + '0') << endl;
		s += (b[i] == b[i - 1] ? last : (3 - (s[(int)s.size() - 1] - '0') + '0'));
		if (s[(int)s.size() - 1] != last) last = s[(int)s.size() - 1];
	}
	p = ' ' + p;
	s = ' ' + s;
	n = (int)s.size() - 1;
	m = (int)p.size() - 1;
//	cout << n << ' ' << m << endl;
//	cout << tn << ' ' << tm << endl;
//	cout << p << endl << s << endl;
	for (int i = 2, j = 0; i <= n; i ++ ) {
		while (j && s[i] != s[j + 1]) j = ne[j];
		if (s[i] == s[j + 1]) j ++ ;
		ne[i] = j;
	}
	for (int i = 1, j = 0; i <= m; i ++ ) {
		while (j && p[i] != s[j + 1]) j = ne[j];
		if (p[i] == s[j + 1]) j ++ ;
		if (j == n) {
			ans1 = i - n + 1;
			break;
		}
	}
	s = "";
	for (int i = 1; i <= tm; ++ i) {
		if (i == 1) {
			s += '2';
			last = '2';
			continue;
		}
//		cout << (s[(int)s.size() - 1] - '0') << ' ';
//		cout << ((3 - (s[(int)s.size() - 1] - '0')) + '0') << endl;
		s += (b[i] == b[i - 1] ? last : (3 - (s[(int)s.size() - 1] - '0') + '0'));
		if (s[(int)s.size() - 1] != last) last = s[(int)s.size() - 1];
	}
	s = ' ' + s;
	n = (int)s.size() - 1;
	m = (int)p.size() - 1;
//	cout << n << ' ' << m << endl;
//	cout << tn << ' ' << tm << endl;
//	cout << p << endl << s << endl;
	for (int i = 2, j = 0; i <= n; i ++ ) {
		while (j && s[i] != s[j + 1]) j = ne[j];
		if (s[i] == s[j + 1]) j ++ ;
		ne[i] = j;
	}
	for (int i = 1, j = 0; i <= m; i ++ ) {
		while (j && p[i] != s[j + 1]) j = ne[j];
		if (p[i] == s[j + 1]) j ++ ;
		if (j == n) {
			if (ans1 == 0) {
				cout << i - n + 1 << endl;
				return 0;
			}
			printf("%d", min(ans1 , i - n + 1));
			return 0;
		}
	}
	cout << ans1 << endl;
	return 0;
}

提交记录

2022/5/12 20:13
加载中...