UVA 12012 - Detection of Extraterrestrial(KMP)
来源:互联网 发布:深红累之渊 知乎 编辑:程序博客网 时间:2024/06/03 19:39
UVA 12012 - Detection of Extraterrestrial
题目链接
题意:给定一个字符串,求其所有子串中,对应1-n循环次数的最长串长度
思路:KMP,n才1000,可以接受O(n^2)的算法,对于每个后缀串,做一次KMP,然后在遍历一遍KMP数组,这样就可以得到每个子串的所有循环次数了,然后不断更新答案即可
代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 1005;int t, ans[N], n, l, r, m, next[N];char str[N], s[N];void getnext() { next[0] = next[1] = 0; int j = 0; for (int i = 2; i <= m; i++) {while (j && s[i - 1] != s[j]) j = next[j];if (s[i - 1] == s[j]) j++;next[i] = j; }}int main() { int cas = 0; scanf("%d", &t); while (t--) {memset(ans, 0, sizeof(ans));scanf("%s", str);n = strlen(str);for (int i = 0; i < n; i++) { m = 0; for (int j = i; j < n; j++)s[m++] = str[j]; getnext(); for (int j = 1; j <= m; j++) {int tmp = j;while (tmp) { if (j % (j - next[tmp]) == 0) {int ti = j / (j - next[tmp]);ans[ti] = max(ans[ti], j); } tmp = next[tmp];} }}printf("Case #%d:", ++cas);for (int i = 1; i <= n; i++) printf(" %d", ans[i]);printf("\n"); } return 0;}
1 0
- UVA 12012 - Detection of Extraterrestrial(KMP)
- uva 12012 - Detection of Extraterrestrial(KMP)
- UVA 12012 - Detection of Extraterrestrial (KMP / 哈希)
- uva 12012 Detection of Extraterrestrial
- Uva 12012 Detection of Extraterrestrial 解题报告
- Uva 12012 Detection of Extraterrestrial 求循环节个数为1-n的最长子串长度 KMP
- 2011ACM上海邀请赛D题(Detection of Extraterrestrial)----KMP
- Detection of Extraterrestrial KMP匹配 重复k次子串 好题
- UVa 12250 - Language Detection
- Detection of Sandboxes
- detection of nose
- Detection of the hidden processes
- Detection of the hidden processes
- Weapons of Mass Destruction, Detection
- Detection of a buffer overrun
- Algorithm of Contour Detection Improvement
- uva 1328 Period(KMP)
- UVA 1328 - Period (KMP)
- Use layout_height of 0dip instead of wrap_content for better Performance
- UncaughtExceptionHandler
- Handler执行时机
- java Volatile
- Queue [Java]
- UVA 12012 - Detection of Extraterrestrial(KMP)
- 解析iphone多线程
- Cocoa程序支持多国语言环境
- Xib文件创建UITableViewCell
- FAT32学习笔记(二)——spec
- iphone开发中使用动态库(dylib)和…
- Hibernate重点知识笔记-必看
- 理解iphone开发中的delegate(委托)
- AVOS Cloud 技术支持系统开源了