[Baltic2014]friends

There are three friends like to play games together, A jun write a string S, B Jun copy it again to obtain T, C-jun in an arbitrary position T (inclusive) inserted to give a character U.
Now you get U , you find S.
the Input
of the first row of a number N, the length of the U is for 100% of the data 2 <= N <= 2000001
second line a string of U, U guaranteed by capital letters
output
output line, If S does not exist, the output "nOT POSSIBLE". If S is not unique, output "nOT uNIQUE". otherwise, output S.
the Sample the Input
7
ABXCABC
the Sample the output
ABC

#include<cmath>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
const int maxn=2000000+10;
using namespace std;
int n,Hash[maxn],tmp[maxn],ans,flag=0,opt;
char str[maxn];
int main ()
{
    scanf("%d%s",&n,str+1);
    if(!(n%2))
    {
        printf("NOT POSSIBLE\n");
        return 0;
    }
    tmp[0]=1;
    for(int i=1;i<=n;i++) 
         Hash[i]=Hash[i-1]*131+str[i],tmp[i]=tmp[i-1]*131;
    unsigned long long x,y;
    for (int i = 1; i <= n; i ++) // Enumeration breaking point 
    {
        if(i<=n/2) 
            x=Hash[n/2+1]-Hash[i]*tmp[n/2+1-i]+Hash[i-1]*tmp[n/2-i+1];
// get the sample is, abxcabc. Now enumerate break point three.
//Hash[n/2+1]-Hash[i]*tmp[n/2+1-i],即abxc-abx=c
//Hash[i-1]*tmp[n/2-i+1]即ab
// then the two together 
        else
            x=Hash[n/2];
// shaped like abcabxc. X direct access to the first half of the character like a 
        if(i<=n/2+1) 
            y=Hash[n]-Hash[n/2+1]*tmp[n/2];
        else
             y=Hash[n]-Hash[i]*tmp[n-i]+(Hash[i-1]-Hash[n/2]*tmp[i-1-n/2])*tmp[n-i];
        if(x==y)
        {
            if(flag && ans!=x)
            {
                printf("NOT UNIQUE\n");
                return 0;
            }
            flag=1;ans=x;opt=i;
        }
    }
    if(!flag)
    {
        printf("NOT POSSIBLE\n");
        return 0;
    }
    int cnt=1;
    for(int i=1;cnt<=n/2;i++)
        if(i!=opt)
           cout<<str[i],cnt++;
    return 0;
}

  

Guess you like

Origin www.cnblogs.com/cutemush/p/12297568.html