Match:Period(POJ 1961)
2016-02-03 23:03
375 查看
Period
题目大意:给定一个字符串,要你找到前缀重复了多少次
思路,就是kmp的next数组的简单应用,不要修正next的距离就好了,直接就可以跳转了
PS:喝了点酒用递归实现除法和取余了。。。结果tle不知道怎么回事。。。
#include <iostream> #include <functional> #include <algorithm> #include <string> using namespace std; typedef int Positon; void Get_Next(const int); static int _next[1000002]; static char str[1000002]; int main(void) { int str_length, k_count, if_res; int case_count = 1; while (~scanf("%d", &str_length)) { if (str_length == 0)break; getchar(); scanf("%s", str); Get_Next(str_length); printf("Test case #%d\n", case_count++); for (int i = 2; i <= str_length; i++) { k_count = i / (i - _next[i]); if_res = i % (i - _next[i]); if (if_res == 0 && k_count > 1) printf("%d %d\n", i, k_count); } cout << endl; } return EXIT_SUCCESS; } void Get_Next(const int str_length) { Positon i = 0, k = -1; _next[0] = -1; while (i < str_length) { if (k == -1 || str[i] == str[k]) { i++; k++; _next[i] = k; } else k = _next[k]; } }
这题用STL的string会卡很长的时间,很奇怪,加了std::ios::sync_with_stdio(false);都没用
相关文章推荐
- 线段树推荐学习博客
- WebAPI接收JSON参数注意事项
- PHP的几个常用加密函数
- 【slighttpd】基于lighttpd架构的Server项目实战(2)—预备知识之libevent
- GitLab致开源项目维护者的一封信
- BZOJ 1053: [HAOI2007]反素数ant dfs
- HDU1083(最大匹配)
- 细说部分servlet知识点(会陆续补充)
- 深入分析Java的序列化与反序列化
- 10.Oracle深度学习笔记——Oracle统计数据自动采集脚本
- c语言:教你一招黑掉别人电脑
- 【转载】oracle索引详解2
- java学习心得——navicat数据库的使用
- 【读书笔记】Android开发艺术探索
- linux下开启mysql慢查询,分析查询语句
- Kmeans实现 java
- Ask Hoegh(4)——select count(*)和select count(1)、count(column)有区别吗?
- Android开发:LeakCanary--检测所有的内存泄漏
- 随机字符串
- 基于MTD的NANDFLASH设备驱动底层实现原理分析(二)