您的位置:首页 > 其它

hackerrank:Palindrome Index(hash)

2015-08-28 15:18 344 查看
题目连接:Palindrome Index

Problem Statement

You are given a string of lower case letters. Your task is to figure out the index of the character on whose removal it will make the string a palindrome. There will always be a valid solution.

In case the string is already a palindrome, then
-1
is
also a valid answer along with possible indices.

Input Format

The first line contains T,
i.e. the number of test cases.

T lines
follow, each containing a string.

Output Format

Print the position (0 index) of the letter by removing which the string turns into a palindrome. For a string, such as
[code]bcbc


we can remove b at index 0 or c at
index 3. Both answers are accepted.

Constraints

1≤T≤20

1≤ length
of string ≤100005

All characters are Latin lower case indexed.

Sample Input

[code]3
aaab
baa
aaa


Sample Output

[code]3
0
-1


Explanation

In the given input, T = 3,

For input aaab, we can see that
removing b from the string makes
the string a palindrome, hence the position 3.

For input baa, removing b from
the string makes the string palindrome, hence the position 0.

As the string aaa is already a palindrome,
you can output 0, 1 or 2 as removal of any of the characters still maintains the palindrome property. Or you can print -1 as this is already a palindrome.

题意:问你从某个位置删除一个字符后剩余的回文

分析:直接字符串hash,但是要判断奇偶啥的,比较麻烦

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int maxn=1e5+100;
const int p=1e6+100;
const int mod=1e9+7;
int ha1[maxn+100],ha2[maxn+100];
int t,len;
char str[maxn];
void Hash()
{
    for(int i=1;i<=len;i++)
        ha1[i]=(ha1[i-1]*1LL*p+str[i])%mod;
    for(int i=len;i>=1;i--)
        ha2[i]=(ha2[i+1]*1LL*p+str[i])%mod;
}
LL Pow(LL a,LL b)
{
    if(b<0)
        return 0;
    LL ans=1;
    while(b)
    {
        if(b&1)
            ans=(ans*a)%mod;
        b>>=1;
        a=(a*a)%mod;
    }
    return ans;
}
int gethash1(int l,int r)
{
    if(l>r) return 0;
    return (ha1[r]-ha1[l-1]*1LL*Pow(p,r-l+1)%mod+mod)%mod;
}
int gethash2(int l,int r)
{
    if(r>l) return 0;
    return (ha2[r]-ha2[l+1]*1LL*Pow(p,l-r+1)%mod+mod)%mod;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",str+1);
        len=strlen(str+1);
        ha1[0]=ha2[0]=ha1[len+1]=ha2[len+1]=0;
        Hash();
        int mid=(len+1)/2,pos=-1;
        if((len&1)&&(ha1[mid-1]==ha2[mid+1]))
            puts("-1");
        else if(ha1[mid]==ha2[mid+1])
            puts("-1");
        else
        {
            if(len&1)
            {
                //cout<<"wocao  "<<endl;
                for(int i=1;i<mid;i++)
                {
                    int s1=ha1[i-1];
                    int s2=gethash1(i+1,mid);
                    int s=(s1*1LL*Pow(p,mid-i)%mod+s2)%mod;
                    if(s==gethash2(len,mid+1))
                    {
                        pos=i;
                        break;
                    }
                }
                if(pos==-1)
                {
                    for(int i=mid+1;i<=len;i++)
                    {
                        int s1=ha2[i+1];
                        int s2=gethash2(i-1,mid);
                        int s=(s1*1LL*Pow(p,i-mid)%mod+s2)%mod;
                        if(s==gethash1(1,mid-1))
                        {
                            pos=i;
                            break;
                        }
                    }
                }
            }
            else
            {
                for(int i=1;i<=mid;i++)
                {
                    int s1=ha1[i-1];
                    int s2=gethash1(i+1,mid);
                    int s=(s1*1LL*Pow(p,mid-i)%mod+s2)%mod;
                    if(s==gethash2(len,mid+2))
                    {
                        pos=i;
                        break;
                    }
                }
                if(pos==-1)
                {
                    for(int i=mid+1;i<=len;i++)
                    {
                        int s1=ha2[i+1];
                        int s2=gethash2(i-1,mid+1);
                        int s=(s1*1LL*Pow(p,i-mid-1)%mod+s2)%mod;
                        if(s==gethash1(1,mid-1))
                        {
                            pos=i;
                            break;
                        }
                    }
                }
            }
            printf("%d\n",pos-1);
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: