lightoj 1258(manacher)
来源:互联网 发布:安卓必备软件 知乎 编辑:程序博客网 时间:2024/06/04 19:41
题意:给出一个字符串,现在可以在这个字符串的右边任意添加字符串使字符串成为回文串,问回文串最短长度。
题解:因为只能在右边添加字符,添加的越少越好,所以要找一个最靠近右端点的回文串,在manacher算法中,如果出现 i + f[i] >= len说明出现了这样的串(最坏情况就是最后一个字母,回文长度是1),计算这个串的回文长度temp,结果就是2 * len - temp。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 1e6 + 1e6 + 100;char str[N];int f[N], pos;int manacher(char *s, int len) { int id = 0; for (int i = len; i >= 0; i--) { s[i + i + 2] = s[i]; s[i + i + 1] = '#'; } s[0] = '$'; int len1 = 2 * len + 1; for (int i = 2; i < len1; i++) { if (f[id] + id > i) f[i] = min(f[id * 2 - i], f[id] - i + id); else f[i] = 1; while (s[i + f[i]] == s[i - f[i]]) ++f[i]; if (f[i] + i > id + f[id]) id = i; if (f[i] + i >= len1) return len1 - i; }}int main() { int t, cas = 1; scanf("%d", &t); while (t--) { scanf("%s", str); int len = strlen(str); int res = manacher(str, len); printf("Case %d: %d\n", cas++, len - res + len); } return 0;}
0 0
- lightoj 1258(manacher)
- lightoj 1258 - Making Huge Palindromes 【Manacher】
- LightOJ 1258 Making Huge Palindromes 【Manacher算法】
- LightOJ 1258 Making Huge Palindromes(manacher算法)
- lightoj 最长回文字符串 1258 (KMP&manacher)
- LightOJ 1258
- Manacher
- manacher
- Manacher
- Manacher
- manacher
- manacher
- manacher
- Manacher
- *Manacher
- Manacher
- LightOJ
- LightOJ
- 关于正则一些探究
- 折半查找插入排序算法
- [UVA 11235]Frequent values[线段树区间查询]
- C#原子操作 Lock用法
- hadooop 之 FileInputFormat 类
- lightoj 1258(manacher)
- c语言二进制八进制和十六进制的总结
- 关于开发定位导航软件中间件的思考
- 数据提供与访问
- 好的开始是成功的一半。
- JSON的结构
- leetcode笔记:Triangle
- C#中IDisposable
- redis 可视化监控工具