Extend to Palindrome - UVa 11475 哈希

Problem E

Extend to Palindromes

Time Limit : 3 seconds

 

 

 

Your task is, given an integer N, to make a palidrome (word that reads the same when you reverse it) of length at least N. Any palindrome will do. Easy, isn't it? That's what you thought before you passed it on to your inexperienced team-mate. When the contest is almost over, you find out that that problem still isn't solved. The problem with the code is that the strings generated are often not palindromic. There's not enough time to start again from scratch or to debug his messy code. Seeing that the situation is desperate, you decide to simply write some additional code that takes the output and adds just enough extra characters to it to make it a palindrome and hope for the best. Your solution should take as its input a string and produce the smallest palindrome that can be formed by adding zero or more characters at its end.

 

 

 

Input

 

 

 

 

Input will consist of several lines ending in EOF. Each line will contain a non-empty string made up of upper case and lower case English letters ('A'-'Z' and 'a'-'z'). The length of the string will be less than or equal to 100,000.

 

 

 

 

 

 

 

Output

 

 

 

For each line of input, output will consist of exactly one line. It should contain the palindrome formed by adding the fewest number of extra letters to the end of the corresponding input string.

 

 

 

 

 

 

 

Sample Input

Sample Output

 

 

 

 

 

aaaa
abba
amanaplanacanal
xyz

aaaa
abba
amanaplanacanalpanama
xyzyx



题意:添加最少的字符使其成为回文串。

思路:正反哈希两次,然后找到相同的字符串,并记录pos值。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
unsigned long long hash1[233333],hash2[233333],mi[233333];
char str[233333];
void Hash(int n)
{ int i;
  hash1[n]=0;
  for(i=n-1;i>=0;i--)
   hash1[i]=hash1[i+1]*3+str[i];
  hash2[0]=0;
  for(i=1;i<=n;i++)
   hash2[i]=hash2[i-1]*3+str[i];
}
int main()
{ int i,j,k,len,pos,ans,pos1,pos2;
  mi[0]=1;
  for(i=1;i<233330;i++)
   mi[i]=mi[i-1]*3;
  while(~scanf("%s",str))
  { len=strlen(str);
    Hash(len);
    ans=-1;
    for(i=0;i<len;i++)
    { if((len-i)%2==1)
      { pos=i+(len-i)/2;
        if(hash1[i]-hash1[pos]*mi[pos-i]!=hash2[len-1]-hash2[pos]*mi[len-1-pos])
         continue;
        ans=i;
        break;
      }
      else if((len-i)%2==0)
      { pos1=(len-i)/2-1;
        pos2=pos1+1;
        if(str[i+pos1]!=str[i+pos2])
         continue;
        if(hash1[i] - hash1[i+pos1]*mi[pos1] != hash2[len-1] - hash2[i+pos2]*mi[len-1-i-pos2])
         continue;
        else
        ans=i;
        break;
      }
    }
    if(ans==-1)
     ans=len-1;
    printf("%s",str);
    for(int i = ans-1;i >= 0;i--)
      printf("%c",str[i]);
        printf("\n");
  }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值