[BalticOI2014] Friends topic:
A string \ (a\)that copies \ (a\) over and inserts a new character at any location to get \ (b\). Give the \ (B (| B|\LE2\TIMES10^6,|\SIGMA|\LE26) \ (a\) , whether it exists, is unique. If unique, find \ (a\).
Ideas:
A string hash.
Source:
#include <cstdio> #include <cctype>inline int getint () {Register char ch; while (!isdigit (Ch=getchar ())); Register int x=ch^ ' 0 '; while (IsDigit (Ch=getchar ())) x= (((x<<2) +x) <<1) + (ch^ ' 0 '); return x;} inline char Getalpha () {Register char ch; while (!isalpha (Ch=getchar ())); return ch;} typedef unsigned long long uint64;const int n=2e6+1;const uint64 base=31;int s[n];uint64 pwr[n],hash[n];inline UInt64 Calc (const int &i,const int &j) {return hash[j]-hash[i-1]*pwr[j-i+1];} int main () {const int n=getint (); if (n%2==0) {puts ("not POSSIBLE"); return 0; } for (register int i=pwr[0]=1;i<=n;i++) {s[i]=getalpha (); Pwr[i]=pwr[i-1]*base; hash[i]=hash[i-1]*base+s[i]-' A ' +1; } UInt64 last=0; int POS; for (register int i=1;i<=n/2;i++) {if (Calc (1,i-1) *pwr[n/2-i+1]+calc (i+1,n/2+1) ==calc (n/2+2,n)) {if (l Ast!=0&&calc (n/2+2,n)!=last) {puts ("not UNIQUE "); return 0; } last=calc (N/2+2,n); Pos=i; }} if (Calc (1,N/2) ==calc (n/2+2,n)) {if (Last!=0&&calc (1,N/2)!=last) {puts ("not UNIQUE"); return 0; } last=calc (1,N/2); pos=n/2+1; } for (register int i=n/2+2;i<=n;i++) {if (Calc (1,N/2) ==calc (n/2+1,i-1) *pwr[n-i]+calc (i+1,n)) {if ( Last!=0&&calc (1,N/2)!=last) {puts ("not UNIQUE"); return 0; } last=calc (1,N/2); Pos=i; }} if (!last) {puts ("not POSSIBLE"); return 0; } if (POS<=N/2) {for (register int i=n/2+2;i<=n;i++) Putchar (S[i]); } else {for (register int i=1;i<=n/2;i++) Putchar (S[i]); } putchar (' \ n '); return 0;}
[Balticoi2014]friends