11和13两个点超了,求助
查看原帖
11和13两个点超了,求助
268811
画船听雨楼主2022/3/4 13:27
#include<iostream>
#include<string>
using namespace std;

int nextt[10000];

int main() {
	string strpro, stract;
	int i = 1, len, j = 0, lenpro, lenact;
	cin >> strpro >> stract;
	lenpro = strpro.length();
	lenact = stract.length();
	len = lenpro - lenact;

	while (i < lenact) {
		if (stract[i] == stract[j]) {
			nextt[i] = j + 1;
			i++;
			j++;
		}
		else {
			if (j)
				j = nextt[j - 1];
			else
				i++;
		}
	}
	i = 0;
	while (i <= len) {
		for (j = 0; j < lenact; j++) {
			if (stract[j] != strpro[j + i]) {
				if (j && nextt[j - 1])
					i += nextt[j - 1];
				else
					i++;
				break;
			}

		}
		if (j == lenact) {
			//cout << "the position is at:" << i << endl;
			cout << i + 1 << endl;
			i++;
		}
	}

	for (i = 0; i < lenact; i++)
		cout << nextt[i] << " ";
	return 0;
}
2022/3/4 13:27
加载中...