前缀数组O(n^3)做法
前缀数组O(n^3)做法
s.substr()的应用非常方便
令string s = "0123456789"; string sub1 = s.substr(5); //只有一个数字5表示从下标为5开始一直到结尾:sub1 = "56789" string sub2 = s.substr(5, 3); //从下标为5开始截取长度为3位:sub2 = "567"
#include<iostream> #include<cstdio> #include<queue> #include<algorithm> #include<cmath> #include<ctime> #include<set> #include<map> #include<stack> #include<cstring> #pragma GCC optimize(2) #define inf 2147483647 #define ls rt<<1 #define rs rt<<1|1 #define lson ls,nl,mid,l,r #define rson rs,mid+1,nr,l,r #define N 1000010 #define For(i,a,b) for(int i=a;i<=b;++i) #define p(a) putchar(a) #define g() getchar() using namespace std; int len; string s; int ans[N]; void in(int &x){ int y=1;char c=g();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=g();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=g();} x*=y; } void o(int x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } int main(){ cin>>s; len=s.size(); For(i,1,len-1) For(k,1,i) if(s.substr(0,k)==s.substr(i-k+1,k)) ans[i]=k; For(i,0,len-1) o(ans[i]),p(' '); return 0; }