[破碎的状态] BZOJ 1031 字符加密
好久没AC题目了..
普通的题做不出来,只好回去做点模版题什么的
模版题还能WA一次自己还是太弱了..
求后缀数组代码是O(n log^2n)的..
void dfs(int t,int n) { if (t>n) return; int i; for (i=0;i<n;i++) { if ((i==0)||(b[i-1]<b[i])) { c[b[i].id]=i; } else { c[b[i].id]=c[b[i-1].id]; } } for (i=0;i<n;i++) { b[i].x=c[b[i].id]; } for (i=0;i<n;i++) { if (b[i].id+t<n) { b[i].y=c[b[i].id+t]; } else { b[i].y=-1; } } sort(b,b+n); dfs(t*2,n); }
代码:
#include<set> #include<map> #include<list> #include<queue> #include<stack> #include<string> #include<time.h> #include<math.h> #include<memory> #include<vector> #include<bitset> #include<fstream> #include<stdio.h> #include<utility> #include<sstream> #include<string.h> #include<iostream> #include<stdlib.h> #include<algorithm> using namespace std; struct node { int x; int y; int id; friend bool operator < (const node &a,const node &b) { if (a.x!=b.x) return a.x<b.x; return a.y<b.y; } }; node b[200005]; char a[200005]; int c[200005]; void dfs(int t,int n) { if (t>n) return; int i; for (i=0;i<n;i++) { if ((i==0)||(b[i-1]<b[i])) { c[b[i].id]=i; } else { c[b[i].id]=c[b[i-1].id]; } } for (i=0;i<n;i++) { b[i].x=c[b[i].id]; } for (i=0;i<n;i++) { if (b[i].id+t<n) { b[i].y=c[b[i].id+t]; } else { b[i].y=-1; } } sort(b,b+n); dfs(t*2,n); } int main() { #ifdef absi2011 freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif scanf("%s",a); int n=strlen(a); int i; for (i=0;i<n;i++) { a[i+n]=a[i]; } for (i=0;i<2*n;i++) { b[i].x=a[i]; b[i].y=-1; b[i].id=i; } sort(b,b+2*n); dfs(1,2*n); for (i=0;i<2*n;i++) { if (b[i].id<n) { printf("%c",a[b[i].id+n-1]); } } printf("\n"); return 0; }
卡常版:
#include<set> #include<map> #include<list> #include<queue> #include<stack> #include<string> #include<time.h> #include<math.h> #include<memory> #include<vector> #include<bitset> #include<fstream> #include<stdio.h> #include<utility> #include<sstream> #include<string.h> #include<iostream> #include<stdlib.h> #include<algorithm> using namespace std; struct node { int x; int y; int id; friend bool operator < (const node &a,const node &b) { //if (a.x!=b.x) return a.x<b.x; return a.y<b.y; } }; bool spj(const node &a,const node &b) { return a.x<b.x; } node b[200005]; char a[200005]; int c[200005]; void dfs(int t,int n) { if (t>n) return; int i; for (i=0;i<n;i++) { if ((i==0)||(b[i-1].x<b[i].x)||((b[i-1].x==b[i-1].x)&&(b[i-1].y<b[i].y))) { c[b[i].id]=i; } else { c[b[i].id]=c[b[i-1].id]; } } for (i=0;i<n;i++) { b[i].x=c[b[i].id]; } for (i=0;i<n;i++) { if (b[i].id+t<n) { b[i].y=c[b[i].id+t]; } else { b[i].y=-1; } } int last=0; for (i=1;i<=n;i++) { if ((i==n)||(b[i-1].x<b[i].x)) { sort(b+last,b+i); last=i; } } dfs(t*2,n); } int main() { #ifdef absi2011 freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif scanf("%s",a); int n=strlen(a); int i; for (i=0;i<n;i++) { a[i+n]=a[i]; } for (i=0;i<2*n;i++) { b[i].x=a[i]; b[i].y=-1; b[i].id=i; } sort(b,b+2*n,spj); dfs(1,2*n); for (i=0;i<2*n;i++) { if (b[i].id<n) { printf("%c",a[b[i].id+n-1]); } } printf("\n"); return 0; }