第17个点TLE
查看原帖
第17个点TLE
326254
LonginusMonkey楼主2023/10/23 21:21

复杂度是对的,有人能帮我卡卡常数吗

#include<bits/stdc++.h>
using namespace std;
int len1; int len2;
int kmp[3001000];
char str[3001000];
int j = 0;
void work(char str2[]) {
	len2 = strlen(str2+1);
	for(int i=len1+1; i<=len1+len2; ++i) {
		str[i] = str2[i-len1];
	}
//	for(int i=1; i<=len1 +len2; ++i) {
//		cout << str[i];
//	}
//	cout << endl;
	int templen = len1+len2;
	int tempj = j;
	for(int i=len1+1; i<=templen; ++i) {
		while(tempj && str[i] != str[tempj+1]) {
			tempj = kmp[tempj];
		}
		if(str[i] == str[tempj+1]) {
			tempj++;
		}
		kmp[i] = tempj;
		cout << kmp[i] << " ";
	}
	cout << endl;
} 
int main() {
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> str+1;;
	len1 = strlen(str+1);
	for(int i=2; i<=len1; ++i) {
		while(j && str[i] != str[j+1]) {
			j = kmp[j];
		}
		if(str[i] == str[j+1]) {
			j++;
		}
		kmp[i] = j;
 	}
	int q; cin >> q;
	while(q--) {
		char str2[12]; cin >> str2+1;
		work(str2);
	}
	return 0;
}
2023/10/23 21:21
加载中...