标签:next get long using 链接 scanf scan etc tar
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 5 inline int read() 6 { 7 int x=0,f=1;char ch=getchar(); 8 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} 9 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} 10 return x*f; 11 } 12 13 /********************************************************************/ 14 15 const int maxn = 3e6+6; 16 char s[maxn]; 17 int Next[maxn]; 18 19 int main(){ 20 21 //int n; n = read(); 22 scanf("%s", s+1); 23 int len = strlen(s+1); 24 Next[1] = 0; 25 int k = 0; 26 for(int i = 2;i <= len;i++){ 27 while(k > 0 && s[k+1] != s[i]){ 28 k = Next[k]; 29 } 30 if(s[k+1] == s[i]) 31 k++; 32 Next[i] = k; 33 } 34 int tmp = 0; 35 int f = 1; 36 for(int i = 1;i <= len;i++){ 37 if(i%(i-Next[i]) == 0){ 38 tmp = (i-Next[i]); 39 } 40 else tmp = 1; 41 if(f){ 42 printf("%d", tmp); 43 f = 0; 44 } 45 else printf(" %d", tmp); 46 } 47 printf("\n"); 48 for(int i = 1;i <= tmp;i++){ 49 printf("%c", s[i]); 50 } 51 printf("\n"); 52 53 return 0; 54 }
标签:next get long using 链接 scanf scan etc tar
原文地址:https://www.cnblogs.com/jaydenouyang/p/9733376.html