UVA11475--Extend to Palindrome
来源:互联网 发布:手机上怎样查淘宝积分 编辑:程序博客网 时间:2024/06/05 20:51
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
题意:在所给的字符串后添加最小字符形成回文串。
思路:建立2个Hash数组,一个从前往后Hash,一个从后往前Hash。
枚举需要添加的字符数i,则后面len-i长度为一个回文串。根据回文串长度的奇偶,分类讨论。
判断对称中心两段的字串是否回文,只需要左端字串的正哈希函数和右断字串的反哈希函数相等。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxn 100080unsigned long long Hash1[maxn],Hash2[maxn],mi[maxn];char str[maxn];void Hash_Get(int n){Hash1[n] = 0;for(int i = n-1;i >= 0;i--)Hash1[i] = Hash1[i+1]*3 + str[i];Hash2[0] = str[0];for(int i = 1;i <= n;i++)Hash2[i] = Hash2[i-1]*3 + str[i];}void init(){mi[0] = 1;for(int i = 1;i < maxn;i++)mi[i] = mi[i-1]*3;}int main(){//freopen("in.txt","r",stdin);init();while(scanf("%s",str)!=EOF){int len = strlen(str);Hash_Get(len);int ans = 100080,pos;for(int i = 0;i < len;i++){if((len-i) & 1){pos = i+(len-i)/2;if(Hash1[i] - Hash1[pos]*mi[pos-i] != Hash2[len-1] - Hash2[pos]*mi[len-1-pos])continue;else {ans = i;break;}}else {if(str[i+((len-i)>>1)] != str[i+(len-i)/2-1])continue;if(Hash1[i] - Hash1[i+(len-i)/2-1]*mi[(len-i)/2-1] != Hash2[len-1] - Hash2[i+((len-i)>>1)]*mi[len-1-(i+((len-i)>>1))])continue;else {ans = i;break;}}}if(ans == maxn)ans = len - 1;printf("%s",str);for(int i = ans-1;i >= 0;i--)printf("%c",str[i]);printf("\n");}return 0;}
- UVA11475--Extend to Palindrome
- UVa11475 - Extend to Palindrome
- uva11475 - Extend to Palindrome KMP
- UVA11475 Extend to Palindrome(哈希)
- UVA11475:Extend to Palindrome(Hash 或 KMP)
- UVa 11475 - Extend to Palindrome
- Extend to Palindrome - UVa 11475 哈希
- UVa 11475-Extend to Palindrome JAVA
- UVA 11475 - Extend to Palindrome(KMP)
- uva 11475 - Extend to Palindrome(KMP)
- UVA 11475 Extend to Palindrome KMP
- UVA 11475 Extend to Palindrome hash
- Extend to Palindrome - UVa 11475 Manacher算法
- UVA - 11475 Extend to Palindrome manacher+贪心
- uva11475
- UVA11475
- UVA 11475 Extend to Palindrome KMP,后缀数组
- Uva 11475 : Extend to Palindrome(Hash或KMP)
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
- ORA-01123:无法启动联机备份;未启用介质恢复(错误分析)
- 一路Hadoop-- 6、在Linux上安装JDK-2,配置jdk的环境变量
- Unity4.3和4.2、4.1版本冲突提示错误解决
- tar 命令
- UVA11475--Extend to Palindrome
- [转载]android—OpenGL—GL10方法解析(转载)
- GUETOJ - 1119 How many ways(动态规划)
- Fedora安装vmware-tools后系统无法正常启动的解决方法
- 插入排序
- wait waitpid WNOHANG 僵尸进程
- 重新认识c++(01),做个简单的复数Complex类
- MSSQL与MySQL语法区别
- ARM ADC程序设计