拓展kmp板题
emm。。。我比较懒 最后一个字母进了vector两个1 不想改了。。。就加了个去重。。。
哈哈
#include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <cmath> #define rap(i, a, n) for(int i=a; i<=n; i++) #define rep(i, a, n) for(int i=a; i<n; i++) #define lap(i, a, n) for(int i=n; i>=a; i--) #define lep(i, a, n) for(int i=n; i>a; i--) #define rd(a) scanf("%d", &a) #define rlld(a) scanf("%lld", &a) #define rc(a) scanf("%c", &a) #define rs(a) scanf("%s", a) #define MOD 10007 #define LL long long #define ULL unsigned long long #define Pair pair<int, int> #define mem(a, b) memset(a, b, sizeof(a)) #define _ ios_base::sync_with_stdio(0),cin.tie(0) //freopen("1.txt", "r", stdin); using namespace std; const int maxn = 1000010, INF = 0x7fffffff; int nex[maxn], ex[maxn]; vector<int> v; void get_next(char *s) { int i = 0, j, po, len = strlen(s); nex[0] = len; v.push_back(len); while(i + 1 < len && s[i] == s[i + 1]) i++; nex[1] = i; po = 1; if(i == len - 1) v.push_back(1); for(int i=2; i<len; i++) { if(i + nex[i-po] < po + nex[po]) nex[i] = nex[i-po]; else { j = po + nex[po] - i; if(j < 0) j = 0; while(i + j < len && s[i + j] == s[j]) j++; nex[i] = j; po = i; if(j == len - i) v.push_back(i); } } } int T, kase = 0; char s[maxn]; int main() { rd(T); while(T--) { v.clear(); rs(s); get_next(s); sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); printf("Case #%d: %d ", ++kase, v.size()); for(int i=0; i<v.size(); i++) { if(i != 0) cout<< " "; cout<< v[i]; } cout<< endl; } return 0; }