UVA – 12467 – Secret Word

Problem NameSecret Word
JudgeUVA
Problem Linkhttps://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3911
Algorithms & DSPrefix Function, KMP
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

vector<ll>pi;
void preFunc(string s)
{
    ll len=s.size();
    pi.resize(len+5);
    pi[0]=0;
    for(ll i=1;i<len;i++){
        ll j=pi[i-1];
        while(j>0&&s[i]!=s[j])j=pi[j-1];
        if(s[i]==s[j])j++;
        pi[i]=j;
    }
}

int main()
{
    ll t;
    cin>>t;
    string s;
    while(t--){
        cin>>s;
        string p=s;
        p+="#";
        reverse(s.begin(),s.end());
        p+=s;
        ll len=p.size();
        preFunc(p);
        ll idx=0;
        for(ll i=(ll)s.size();i<len;i++){
            idx=max(idx,pi[i]);
        }
        for(ll i=idx-1;i>=0;i--){
            cout<<p[i];
        }
        cout<<"\n";
    }
    return 0;
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
error: Alert: Content is protected !!