题解:
二分最近的不相同
然后hash判断是否相同
然后贪心
代码:
#include<bits/stdc++.h> using namespace std; #define ull unsigned long long const int N=31000; int n; char s[N],s1[11],ans[N]; ull h1[N],h2[N],pw[N]; int main() { scanf("%d",&n); for (int i=1;i<=n;i++)scanf("%s",s1+1),s[i]=s1[1]; pw[0]=1; for (int i=1;i<=n;i++)pw[i]=pw[i-1]*133; for (int i=1;i<=n;i++)h1[i]=h1[i-1]*133+s[i]; for (int i=n;i>=1;i--)h2[i]=h2[i+1]*133+s[i]; for (int i=1,l1=1,r1=n;i<=n;i++) { int l=1,r=r1-l1+1; while (l<=r) { int mid=(l+r)>>1; if (h1[l1+mid-1]-h1[l1-1]*pw[mid]==h2[r1-mid+1]-h2[r1+1]*pw[mid])l=mid+1; else r=mid-1; } if (l>r1-l1+1)l=1; if (s[l1+l-1]>s[r1-l+1])ans[i]=s[r1],r1--; else ans[i]=s[l1],l1++; } for (int i=1;i<=n;i++) { putchar(ans[i]); if (i%80==0)puts(""); } return 0; }