【 哈希和哈希表】Three Friends【進制哈希】

Three Friends

傳送門:鏈接 (UPC)或  鏈接(大視野)

題目描述

Three friends like to play the following game. The first friend chooses a string S. Then the second friend constructs a new string T that consists of two copies of the string S. finally, the third friend inserts one letter at the beginning, the end or somewhere inside the string T, thereby creating a string U.
You are given the string U and your task is to reconstruct the original string S.

輸入

The first line of the input contains N(2 ≤ N ≤ 2000001), the length of the final string U. The string U itself is given on the second line. It consists of N uppercase English letters (A, B, C, . . . , Z).

輸出

Your program should print the original string S. However, there are two exceptions:
1.If the final string U could not have been created using the above procedure, you should print NOT POSSIBLE.
2.If the original string S is not unique, you should print NOT UNIQUE.

樣例輸入

7
ABXCABC

樣例輸出

ABC

題目大意:

有三個字符串分別爲S,T,U,其中T=S+S,U爲在T中插入一個字符。

現在給出字符串U,判斷是否存在原始字符串S,或者是存在不止一個。

aaa 應該輸出 a ,而不應該判斷爲不存在。

解題思路:

hash應該能想到,但我T了n遍

1、數據量很大,最好用scanf或者加ios::sync_with_stdio(false);。

2、暴力每個點,判斷這個點的字符去掉後左右兩邊的hash值是否相等。

3、解決aaa的問題,我開始是用map標記,但因爲map要用string做key鍵,導致用了很多string函數導致超時,解決這個問題其實可以保存第一個符合條件的hash值,以後直接判斷

4、在遍歷過程中,一旦有兩個hash值不相同的點且都滿足條件,立刻輸出並return 0也是優化的關鍵。

AC代碼:

我用的hash方法是進制哈希(我沒取模肯定會導致爆long long,建議你們取模):詳解進制哈希

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX=2e6;
LL k=27;
LL ha[MAX+5];
LL qpow(LL m,LL q)
{
    LL ans=1;
    while(q){
        if(q%2) ans*=m;
        m*=m;
        q/=2;
    }
    return ans;
}
void hasha(string a)
{
    LL la=a.size();
    la-=1;
    for(LL i=la;i>=1;i--){
        ha[i]=ha[i+1]*k+a[i];
    }
}

int main()
{
    ios::sync_with_stdio(false);
    LL la;
    string a;
    cin>>la>>a;
    a=' '+a;
    if(la%2==0){
        cout<<"NOT POSSIBLE"<<endl;
        return 0;
    }
    hasha(a);
    LL flag=-1;
    long long flag1;
    for(LL i=1;i<=la;i++){
        if(i==(la/2+1)){
            if((ha[1]-ha[i]*qpow(k,i-1)==(ha[i+1]))){
                if(flag==-1){
                    flag1=ha[i+1];
                    flag=i;
                }else{
                    if(ha[i+1]!=flag1){
                        cout<<"NOT UNIQUE"<<endl;
                        return 0;
                    }
                }
            }
        }else if(i<(la/2+1)){
            LL x=i-1;
            LL y=la/2-i+1;
            LL num1=la/2+2;
            if(((ha[i+1]-ha[num1]*qpow(k,y))*qpow(k,x)+(ha[1]-ha[i]*qpow(k,x)))==ha[num1]){
                if(flag==-1){
                    flag1=ha[num1];
                    flag=i;
                }else{
                    if(ha[num1]!=flag1){
                        cout<<"NOT UNIQUE"<<endl;
                        return 0;
                    }
                }
            }
        }else if(i>(la/2+1)){
            LL x=i-la/2-1;
            LL num1=la/2+1;
            if(ha[i+1]*qpow(k,x)+(ha[num1]-ha[i]*qpow(k,x))==(ha[1]-ha[num1]*qpow(k,la/2))){
                if(flag==-1){
                    flag1=ha[1]-ha[num1]*qpow(k,la/2);
                    flag=i;
                }else{
                    if((ha[1]-ha[num1]*qpow(k,la/2))!=flag1){
                        cout<<"NOT UNIQUE"<<endl;
                        return 0;
                    }
                }
            }
        }
    }
    if(flag==-1) cout<<"NOT POSSIBLE"<<endl;
    else{
        LL dir=flag,j=1;
        for(LL i=1;;i++){
            if(i==dir) continue;
            else{
                cout<<a[i];
                j++;
            }
            if(j==la/2+1) break;
        }
        cout<<endl;
    }
    return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章