Problem - 1358

  KMP求循环节次数。题意是,给出一个长度为n的字符串,要求求出循环节数大于1的所有前缀。可以直接用KMP的方法判断是否有完整的k个循环节,同时计算出当前前缀的循环节的个数。

代码如下:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
char buf[N];
int next[N]; void getNext(char *str, int len) {
int i = , j = -;
next[] = -;
while (i <= len) {
while (j > - && str[i] != str[j]) j = next[j];
i++, j++;
next[i] = j;
}
} typedef pair<int, int> PII;
PII rec[N]; int main() {
int n, cas = ;
while (cin >> n && n) {
cin >> buf;
getNext(buf, n);
// for (int i = 0; i <= n; i++) cout << next[i] << ' '; cout << endl;
int cnt = ;
for (int i = ; i <= n; i++) {
if (i % (i - next[i])) continue;
if (i / (i - next[i]) == ) continue;
rec[cnt++] = PII(i, i / (i - next[i]));
}
sort(rec, rec + cnt);
printf("Test case #%d\n", cas++);
for (int i = ; i < cnt; i++) {
printf("%d %d\n", rec[i].first, rec[i].second);
}
puts("");
}
return ;
}

——written by Lyon

hdu 1358 Period (KMP求循环次数)的更多相关文章

  1. Hdu 1358 Period (KMP 求最小循环节)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...

  2. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

  3. [HDU 1358]Period[kmp求周期]

    题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...

  4. HDU 1358 Period KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1358 求周期问题,简单KMP—— AC代码: #include <iostream> # ...

  5. HDU 1358 Period(KMP next数组运用)

    Period Problem Description For each prefix of a given string S with N characters (each character has ...

  6. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  7. HDU 1358 Period(KMP计算周期)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...

  8. hdu 1358 Period(KMP入门题)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. HDU - 1358 - Period (KMP)

    Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

随机推荐

  1. Leetcode671.Second Minimum Node In a Binary Tree二叉树中的第二小结点

    给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中 ...

  2. 如何正确的在Android中存储特定应用文件

    原文地址:How to Correctly Store App-Specific Files in Android Christophe Versieux (Waza_be)发表了一篇rant abo ...

  3. HDU 2639 第K大背包问题

    //状态方程和01背包类似,dp[j][k]表示背包容量为j的第k大背包的值.......... //应当注意的是此时dp[j][1.....k]应当是递减的.................... ...

  4. LintCode 删除排序链表中的重复元素

    给定一个排序链表,删除所有重复的元素每个元素只留下一个. 样例 给出 1->1->2->null,返回 1->2->null 给出 1->1->2->3 ...

  5. Python datetime对象的方法

  6. laravel学习文档

    https://github.com/barryvdh/laravel-debugbar Laravel 精选资源大全 http://laravelacademy.org/post/153.html ...

  7. 数据库lib7第4题创建存储过程

    1. 传入2个字符串变量,其中,每个字符串是用分号(:)分隔的字串形式, 比如str1=’ab12;ab;cccc;tty’, str2=’1;6sf;8fffff;dd’, 注意,字符串是用户输入的 ...

  8. k8s 超详细总结,面试必问

    一个目标:容器操作:两地三中心:四层服务发现:五种Pod共享资源:六个CNI常用插件:七层负载均衡:八种隔离维度:九个网络模型原则:十类IP地址:百级产品线:千级物理机:万级容器:相如无亿,K8s有亿 ...

  9. 支付宝sdk iOS 集成

    1添加支付宝源文件和库文件AlipayOrder.h    AlipayOrder.m    AlipayResult.h    AlipayResult.m  AlixLibService.h   ...

  10. execute和submit的区别与联系

    execute和submit都属于线程池的方法,execute只能提交Runnable类型的任务,而submit既能提交Runnable类型任务也能提交Callable类型任务. execute会直接 ...