poj 2406 Power Strings(KMP入门,next函数理解)
2015-08-10 17:30
543 查看
Power Strings
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 37685 | Accepted: 15590 |
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
Output
For each s you should print the largest n such that s = a^n for some string a.
Sample Input
abcd aaaa ababab .
Sample Output
1 4 3
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
Source
Waterloo local 2002.07.01
基础题目,只要是理解next函数。
#include<iostream> #include<vector> #include <cstdio> #include <cstring> #include <cstdlib> #include <math.h> #include<algorithm> #define ll long long #define eps 1e-8 using namespace std; int nexts[1050]; char b[1050]; void pre_nexts(int m) { memset(nexts,0,sizeof(nexts)); int j = 0,k = -1; nexts[0] = -1; while(j < m) { if(k == -1 || b[j] == b[k]) nexts[++j] = ++k; else k = nexts[k]; } } int main(void) { while(scanf("%s",b),b[0] != '.') { int n = (int)strlen(b); pre_nexts(n); if(n % (n-nexts ) == 0 && n/(n - nexts ) > 1) printf("%d\n",n/(n-nexts )); //根据next函数的最后一个匹配数,算出循环节点~ else printf("1\n"); } return 0; }
相关文章推荐
- Ajax.BeginForm使用与注意点 IN MVC
- 选择、搜索国家码&数据库表转Json文件
- HDOJ-2009-求数列的和
- php如何将webp格式图片转为jpeg(ImageMagick+libwebp)
- Oracle Error Handling(Oracle 异常处理)
- C++中new是否进行初始化的问题
- LCS算法的概述及由来(LCS)最长公共子序列
- solr 的安装与使用、中文分词器的配置
- 软件系统架构中的分层思想
- windows 平台开发的JSP程序上传到linux要注意的事项
- iOS 国外 sdk 分享
- GPS Sensor Driver For Win7&Win8 For USB接口
- 通用券抢券的优化过程.
- iOS将汉字转换为拼音
- C#集合类型
- 【姿势】Android 和 Mac 爆毁灭级安全漏洞
- W25Q128的SPI 驱动
- 树结构练习——判断给定森林中有多少棵树
- java.lang.IllegalStateException: Optional int parameter 'currentSpecialIndex' is present but cannot
- Let the Balloon Rise