poj 2406 Power Strings(KMP入门,next函数理解)
2024-09-06 07:04:53
Power Strings
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 37685 Accepted: 15590 Description
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
3Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.Source
基础题目,只要是理解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[];
char b[]; void pre_nexts(int m)
{
memset(nexts,,sizeof(nexts));
int j = ,k = -;
nexts[] = -;
while(j < m)
{
if(k == - || b[j] == b[k]) nexts[++j] = ++k;
else k = nexts[k];
}
}
int main(void)
{
while(scanf("%s",b),b[] != '.')
{
int n = (int)strlen(b);
pre_nexts(n);
if(n % (n-nexts[n]) == && n/(n - nexts[n]) > ) printf("%d\n",n/(n-nexts[n]));
//根据next函数的最后一个匹配数,算出循环节点~
else printf("1\n");
}
return ;
}
最新文章
- BPM业务流程管理与SAP如何更好集成整合?
- Oracle 应用于.NET平台
- linux下安装编译php的curl扩展
- Linux中记录终端(Terminal)输出到文本文件(转载)
- VC++中,如何定义callback函数和它的触发事件?
- Java按正则提取字符串
- 网络资源(5) - Android视频
- github上fork了别人的项目后,再同步更新别人的提交
- [LeetCode] Decode Ways II 解码方法之二
- Day039--HTML
- asp:DropDownList 使用
- B. Yet Another Array Partitioning Task ——cf
- Xeon Phi 《协处理器高性能编程指南》随书代码整理 part 3
- 研究VCL源码的原因和起点
- 【bzoj3680】平衡点 模拟退火
- 相关系数(CORRELATION COEFFICIENTS)会骗人?
- cxGrid 颜色设置
- Windows下文件检索的基本姿势
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
- PAT 1058 选择题(20)(代码+思路)