BZOJ4974 字串大師(kmp)
阿新 • • 發佈:2018-12-02
顯然最短迴圈節長度=i-next[i],則相當於給定next陣列構造字串。然後按照kmp的過程模擬即可。雖然這看起來是一個染色問題,但是由圖的特殊性,如果next=0只要貪心地選最小的就可以了,然而並不會證。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> #include<cassert> using namespace std;#define ll long long #define N 100010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,nxt[N]; bool flag[26]; char s[N]; int main() { #ifndef ONLINE_JUDGE freopen("bzoj4974.in","r",stdin); freopen("bzoj4974.out","w",stdout); const charLL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif n=read(); for (int i=1;i<=n;i++) nxt[i]=i-read(); nxt[0]=-1; for (int i=1;i<=n;i++) { int j=nxt[i-1];memset(flag,0,sizeof(flag)); while (~j&&j+1!=nxt[i]) flag[s[j+1]-'a']=1,j=nxt[j]; if (j==-1) {for (int j=0;j<26;j++) if (!flag[j]) {s[i]=j+'a';break;}} else s[i]=s[j+1]; } printf("%s",s+1); return 0; }