POJ-3974 , HDU-3294, Hdu-3613(manacher)

题目链接: Palindrome

思路

简单的manacher模板,不多说.

#include 
#include 
#include 
using namespace std;
char s[2010001];
int P[2010001];
int id,miner;
int manacher(char s[])
{
    int i,len = strlen(s);
    for(i = len ; i >= 0 ; i--)
    {
        s[2*i+2] = s[i];
        s[2*i+1] = '#';
    }
    s[0] = '*';
    for(i = 2 ;i <= len*2 ; i++)
    {
        if(id + P[id] > i)
        P[i] = min( P[2*id-i], P[id]+id-i);
        else
        P[i] = 1;
        while(s[i+P[i]] == s[i-P[i]])
        ++P[i];
        if(id + P[id] < i + P[i])
        id = i;
        if(P[i] > miner)
        miner = P[i];
    }
    return miner - 1;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    int caser = 1;
    while(cin >> s)
    {
        id = 0, miner = 0;
        if(s[0] == 'E')
            break;
        cout <<"Case " <<caser++ << ": ";
        cout << manacher(s) <<endl;
    }
}

题目链接: Girls’ research

思路

开始的时候把字母转换那里理解错了,所以WA了几次,至于找左端点,我分为P[i]是奇数还是偶数,自己写写几个例子就出来了。

#include 
#include 
#include 
#include 
using namespace std;
char s[401000],c[200001],s1;
int P[401000],l,r,id,miner;
map<char,char>M;
int manacher(char s[])
{
    int len = strlen(s),i,p;
    for( i = len ; i >= 0 ; i--)
    {
        s[2*i+2] = s[i];
        s[2*i+1] = '#';
    }
    s[0] = '*';
    for( i = 2 ; i <= 2*len ; i++)
    {
        if(id + P[id] > i)
        P[i] = min( P[2*id-i] ,P[id]+id-i);
        else
        P[i] = 1;
        while(s[i+P[i]] == s[i-P[i]])
        ++P[i];
        if(id+P[id] < i+P[i])
        id = i;
        if(P[i] > miner)
        {
            miner = P[i];
            int p = (P[i]-1)/2;
            if(P[i] % 2)
            l = (i - 1) - 2 * (p - 1);
            else
            l = i - 2*p;
        }
    }
    return miner-1;
}
void turnover()
{
    int i;
    M.clear();
    for(i = 0 ; char(s1 + i) <= 'z'; i++)
    {
        M[char(s1+i)] = char('a' + i);
    }
    char p;
    for( p = 'a' ; p < s1 ; p++ )
    {
        M[p] = char('a'+ i);
        i++;
    }
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    int c,i;
    while(cin >> s1)
    {
    cin >> s;
    turnover();
    miner = 0 ,id = 0;
    int c = manacher(s);
    if(c == 1)
        cout << "No solution!" << endl;
    else
    {
        cout << (l-2)/2 << ' ' << (l-2)/2+c-1 << endl;
        for(i = l ;i <= l+(c-1)*2 ;i=i+2)
        cout << M[s[i]];
        cout << endl;
    }
    }
}

题目链接: Best Reward

思路

因为细节还是WA了好几次,关键在于我们对于得到的P[]数组的应用,对于已经改动过的字符数组s,我们已经得到了每个P[i]值,接下来就是枚举了。对于改动过的字符数组,我们第一次要使得left = 2,也就是说把s[2]隔开,那么如果P[left] = left 说明前面隔开的是一个回文串,我们就把他的价值加上,left只是前面被隔开的字符串的中间那个字符,因此应该加上sum[2*left-1],然后我们再判断后边的字符串,后面字符串中间位置是(i+len-1)/2,如果P[right] +right == len 说明后面的是回文串。

#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 10001000;
char s[maxn];
int P[maxn];
int sum[maxn];
map<char,int>M;
int id;
int manacher(char s[])
{
    int len = strlen(s),i;
    for(i = len ; i >= 0 ; i--)
    {
        s[2*i+2] = s[i];
        s[2*i+1] = '#';
    }
    s[0] = '*';
    for(i = 2 ; i <= 2*len ; i++)
    {
        if(id+P[id] > i)
        P[i] = min(P[2*id-i],P[id]+id-i);
        else
        P[i] = 1;
        while(s[i + P[i]] == s[i - P[i]])
        ++P[i];
        if(id+P[id] < i+P[i])
        id = i;
    }
}
int main()
{
    int t ,i,A,tot,ans,value[27];
    cin >> t;
    while(t--)
    {
       ans = 0;
       tot = 0 ;
       id = 0;
       M.clear();
       for(i = 0 ;i < 26 ; i++)
       {
           cin >> value[i];
       }
    cin >> s;
    manacher(s);
    sum[0] = 0;
    int len = strlen(s);
    for( i = 1 ; i < len ; i++ )
    {
        if(s[i] == '#')  sum[i] = sum[i-1];
        else sum[i] = sum[i-1] + value[s[i]-'a'];
    }
    int ans1 = 0;
    for (i = 4 ;i < len ; i = i+2)
    {
        ans = 0;
        int l = i / 2;
        if(P[l] == l)
        ans += sum[2*l-1];
        int r = (len -1 + i)/2;
        if(P[r] + r == len)
        ans += sum[len-1] - sum[2*l-1];
        ans1 = max(ans1,ans);
    }
    cout << ans1<< endl;
    }
}

你可能感兴趣的:(manacher)