Three friends

https://loj.ac/problem/2823

Title Description

  Given a string U, seeking to satisfy the condition of the string S so as to copy it again after a character string insert is U.

Thinking

  Through the insertion of a character, so we can scan, each scan when the characters removed, after removing the character string is divided into two sections of equal length, if the two are identical then compare this to. If violence enumeration to see if the same will certainly fly T, we consider the use of string Hash, Hash value obtained is equal to two comparisons. Note that there are two points: ① attention to remove the binary string conversion; ② there may exist a plurality of equal S, they are not repeated.

Code

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull p=47;
char s[2000005];
ull sum[2000005],power[2000005];
int main() 
{
//    freopen("aa.txt","r",stdin);
    int n;
    scanf("%d",&n);
    scanf(" %s",s+1);
    if(n%2==0){printf("NOT POSSIBLE");return 0;}
    int len=(n-1)/2;
    power[0]=1;
    for(int i=1;i<=n;i++)
    {
        sum[i]=sum[i-1]*p+s[i]-'A';
        power[i]=power[i-1]*p;
    }
    ull ans=0,k=0,l=0;
    bool f=1;
//    cout<<sum[4]<<endl;
    for(int i=1;i<=n;i++)
    {
        if(i<=len+1)
        {
            ull s1=sum[i-1]*power[len-i+1]+(sum[len+1]-sum[i]*power[len-i+1]);
            ull s2=sum[n]-sum[n-len]*power[len];
            if(s1==s2)
            {
                ans++;
                if(ans==1)k=s1,l=i;
                else if(k!=s1)f=0;
            }
//            cout<<i<<' '<<s1<<' '<<s2<<' '<<ans<<endl;
        }
        else
        {
            ull s1=sum[len];
            ull s2=(sum[i-1]-sum[len]*power[i-len-1])*power[len*2-i+1]+sum[n]-sum[i]*power[n-i];
            if(s1==s2)
            {
                ans++;
                if(ans==1)k=s1,l =i;
                else if(k!=s1)f=0;
            }
//            cout<<i<<' '<<s1<<' '<<s2<<' '<<ans<<endl;
        }
    }
//    cout<<ans<<' '<<l<<endl;
    if(ans==1||(ans>1&&f))
    {
        if(l<=len+1)
            for(int i=n-len+1;i<=n;i++)
                putchar(s[i]);
        ); i <= len; i ++1i =int(forelse
            
                putchar(s[i]);
    }
    else if(ans==0)
        printf("NOT POSSIBLE");
    else printf("NOT UNIQUE");
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/fangbozhen/p/11619392.html