变为回文串

Description:

我们现在给你一个字符串,我们规定只能在这个字符串的右边添加字符,问最少添加多少字符可以使这个字符串变为回文串?

Input

第一行一个正整数T<=10代表测试数据个数
每组测试数据给出一个字符串S,保证|S|<=1000000,且只包含小写字母

Output

对每组测试数据,输出最少需要添加的字符的个数

Sample Input

3
ab
aba
abad

Sample Output

1
0
3
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef unsigned long long ll;
const int base=51;
const int maxn=1100000;
ll hash[maxn],hash1[maxn];
ll xp[maxn];
char str[maxn];
int len;
int main()
{
  int t,i;
  cin>>t;
  xp[0]=1;
    for(int i=1;i<maxn;i++)
      xp[i]=xp[i-1]*base;
    getchar();
    while(t--){
      memset(hash,0,sizeof(hash));
        memset(hash1,0,sizeof(hash1));    
      scanf("%s",str);
      len=strlen(str);
        for(int i=len-1;i>=0;i--)
        {
            hash[i]=hash[i+1]*base+str[i]-'a'+1;
            hash1[i]=hash1[i+1]*base+str[len-i-1]-'a'+1;
        }
        int i;
        for(i=0;i<len;i++){
          if(hash1[0]-hash1[len-i]*xp[len-i]==hash[i])break;
        }
        printf("%d\n",i);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值