manacher算法 洛谷3805 manacher
来源:互联网 发布:苹果mac自有办公软件 编辑:程序博客网 时间:2024/06/16 17:54
Description
给出一个只由小写英文字符a,b,c…y,z组成的字符串S,求S中最长回文串的长度.
字符串长度为n
Input
一行小写英文字符a,b,c…y,z组成的字符串S
Output
一个整数表示答案
Data Constraint
字符串长度len <= 11000000
Solution
manacher裸题,纯粹找手感
Code
#include <stdio.h>#include <string.h>#define rep(i, st, ed) for (int i = st; i <= ed; i += 1)#define min(x, y) (x)<(y)?(x):(y)#define max(x, y) (x)>(y)?(x):(y)#define L 22000003int rad[L];char rd[L], str[L];int main(void){ scanf("%s", rd); int len = strlen(rd); str[0] = '#'; rep(i, 0, len - 1){ str[i * 2 + 1] = rd[i]; str[i * 2 + 2] = '#'; } len = len * 2 + 1; int pos = 0, mx = 0; int ans = 0; rep(i, 0, len){ rad[i] = 0; if (i < mx){ rad[i] = min(rad[pos - (i - pos)], mx - i); } while (i + rad[i] + 1 < len && i - rad[i] > && str[i + rad[i] + 1] == str[i - rad[i] - 1]){ rad[i] += 1; } if (rad[i] + i > mx){ mx = rad[i] + i; pos = i; } ans = max(ans, rad[i]); } printf("%d\n", ans); return 0;}
阅读全文
0 0
- manacher算法 洛谷3805 manacher
- Manacher算法
- Manacher算法
- Manacher算法
- Manacher算法
- Manacher 算法
- manacher算法
- manacher 算法
- Manacher算法
- manacher算法
- manacher算法
- manacher算法
- Manacher 算法
- Manacher算法
- manacher算法
- Manacher算法
- manacher算法
- Manacher算法
- 模板类的使用
- rabbitmq 交换模式-Direct
- Knuth-Morris-Pratt Algorithm
- ssh使用加密方式
- <mvc:annotation-driven/>
- manacher算法 洛谷3805 manacher
- 面试总结
- js函数式编程
- java编程中遇到的时区与时间问题总结
- HDU
- C语言排序之堆排序篇
- Qt Creator使用
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 将数据库中的数据导入Solr索引库