poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 14653 | Accepted: 6965 |
Description
Input
number zero on it.
Output
Sample Input
- 3
- aaa
- 12
- aabaabaabaab
- 0
Sample Output
- Test case #1
- 2 2
- 3 3
- Test case #2
- 2 2
- 6 2
- 9 3
- 12 4
题意:求前缀的长度,以及其中最小循环节的循环次数 但是循环次数必须大于1
- #include<stdio.h>
- #include<string.h>
- #define MAX 1000100
- int next[MAX];
- char str[MAX];
- int a[MAX],b[MAX];
- int n,m,k;
- void getfail()
- {
- int i,j;
- next[0]=next[1]=0;
- for(i=1;i<n;i++)
- {
- j=next[i];
- while(j&&str[i]!=str[j])
- j=next[j];
- next[i+1]=str[i]==str[j]?j+1:0;
- }
- }
- void kmp()
- {
- int i,j;
- for(i=1;i<=n;i++)
- {
- k=i;
- if(k==(k-next[k]))//k-next[k]最小循环节长度
- continue;
- if(k%(k-next[k])==0)
- printf("%d %d\n",i,k/(k-next[k]));
- }
- printf("\n");
- }
- int main()
- {
- int i,t=1;
- while(scanf("%d",&n),n)
- {
- getchar();
- for(i=0;i<n;i++)
- scanf("%c",&str[i]);
- getfail();
- printf("Test case #%d\n",t++);
- kmp();
- }
- return 0;
- }
poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】的更多相关文章
- KMP POJ 1961 Period
题目传送门 /* 题意:求一个串重复出现(>1)的位置 KMP:这简直和POJ_2406没啥区别 */ /******************************************** ...
- [KMP求最小循环节][HDU1358][Period]
题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...
- KMP + 求最小循环节 --- POJ 2406 Power Strings
Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少 ...
- hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)
传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...
- HDU 3746 (KMP求最小循环节) Cyclic Nacklace
题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...
- [KMP求最小循环节][HDU3746][Cyclic Nacklace]
题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...
- poj 2406 Power Strings【字符串+最小循环节的个数】
Po ...
- HDU 1358 Period 求前缀长度和出现次数(KMP的next数组的使用)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- poj 1961 Period 【KMP-next前缀数组的应用】
题目地址:http://poj.org/problem?id=1961 Sample Input 3 aaa 12 aabaabaabaab 0 Sample Output Test case #1 ...
随机推荐
- PHP代码批量加密
<?php error_reporting(E_ALL); ini_set('display_errors','1'); //批量加密码当前目录 $dirnow = getcwd(); $dir ...
- opencv 常用函数介绍
××××××××××××××××××××××××××××××××××××××× CvScalar imgmean,imgstd; double imgmax,imgmin; cvAvgSdv(img, ...
- Codeforces 543B Destroying Roads(最短路)
题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...
- js 表达式与运算符 详解(下)
比较运算符: > .>= .<. <=. ==. !=. ===. !==. 比较运算符的结果都为布尔值 ==只比较值是否相等 而 ===比较的是值和数据类型都要 ...
- mysql数据类型——整型INT(m)
1.整形分为四种 tinyint smallint mediumint int bigint 注意: 右侧的取值范围是在未加unsigned关键字的情况下,如果加了unsigned,则最大值翻倍,如t ...
- smarty中判断一个变量是否存在于一个数组中或是否存在于一个字符串中?
smarty支持php的系统函数可以直接使用{if in_array($str, $arr) || strpos($str, $string)} yes {else} no{/if}
- Python Tutorial 学习(九)--Classes
## 9. Classes 类 Compared with other programming languages, Python's class mechanism adds classes wit ...
- Whitespace character
In computer science, whitespace is any character or series of whitespace characters that represent h ...
- Js使用word书签填充内容
Js使用word书签填充内容 1.在模板文件中需要填充的地方插入书签 填充内容为:(|光标所在处) 填写书签名,点击添加完成: 2.使用js打开模板,获取书签位置,填充数据: function pri ...
- myeclipse spket spket-1.6.23.jar 破解安装教程
一年前安装文档就写过了,今天写破解文档,本来开发js/ext是想用aptana的,但是安装包100多M,我还是用spket吧(才11M),这个需要破解一下license,否则用不了. 一 安装教程如下 ...