uva 11475 - Extend to Palindrome(KMP)
2016-01-14 11:02
405 查看
题目链接:uva 11475 - Extend to Palindrome
题目大意:给定一个字符串,输出最少须要加入多少个字符使得字符串变成回文串。
解题思路:以字符串的转置做KMP,然后用原串匹配就可以。最后匹配长度即为反复长度。
题目大意:给定一个字符串,输出最少须要加入多少个字符使得字符串变成回文串。
解题思路:以字符串的转置做KMP,然后用原串匹配就可以。最后匹配长度即为反复长度。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1e5+5; char s[maxn], t[maxn]; int n, jump[maxn]; void get_jump () { int p = 0; for (int i = 2; i <= n; i++) { while (p && s[p + 1] != s[i]) p = jump[p]; if (s[p + 1] == s[i]) p++; jump[i] = p; } } int find () { int p = 0; for (int i = 1; i <= n; i++) { while (p && s[p + 1] != t[i]) p = jump[p]; if (s[p + 1] == t[i]) p++; } return p; } int main () { while (scanf("%s", s + 1) == 1) { printf("%s", s+1); n = strlen(s + 1); for (int i = 1; i <= n + 1; i++) t[i] = s[i]; reverse(s + 1, s + n + 1); get_jump(); int k = find(); for (int i = k + 1; i <= n; i++) printf("%c", s[i]); printf("\n"); } return 0; }
相关文章推荐
- html关于强制显示、隐藏浏览器的滚动条,解决由于高度变化带来的滚动条影响页面宽度的问题
- 新浪微博第二天
- 项目管理
- linux命令行-wget
- Fragment Touch事件泄露
- Clustered Data ONTAP Fundamentals课程第一单元学习笔记(续3)
- 如何从0到1搭建电商促销系统
- NoteExpress中文件夹名输入为中文时显示为“??”的解决办法
- web开发的三大基本语言
- 大家好!
- Mac下Git命令汇总
- eclipse maven 下添加jar包
- java--异常处理机制
- JavaScript学习总结1
- mysql 创建库时指定编码
- JSONModel 使用
- 使用diskpart命令修复U盘分区
- clouderamagner 中hue 工作流调用sqoop1 问题处理
- javascript检测flash插件是否被禁用的方法
- ScrollView起始位置不是最顶部的解决办法