POJ
来源:互联网 发布:mac版永恒战士2存档 编辑:程序博客网 时间:2024/05/18 08:40
题目链接:http://poj.org/problem?id=2406
题意:给出一个字符串 问它最多由多少相同的字串组成。
思路:用next数组,如果n%(n-next[n])==0,则存在重复连续子串,长度为n-next[n],如果不存在那么就只能使循环节为自己。
代码:
#include <cstdio>#include <cmath>#include <iostream>#include <cstring>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <map>#include <numeric>#include <set>#include <string>#include <cctype>#include <sstream>#define INF 0x3f3f3f3f#define INF 0x3f3f3f3f#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1using namespace std;typedef long long LL;typedef pair<LL, LL> P;const int maxn = 1e6 + 5;const int mod = 1e8 + 7;int Next[maxn];char T[maxn];int n;void getNext() { int j, k; j = 0; k = -1; Next[0] = -1; while(j < n) if(k == -1 || T[j] == T[k]) Next[++j] = ++k; else k = Next[k];}int main() { //freopen ("in.txt", "r", stdin); while (~scanf ("%s", T)) { if (T[0]=='.') break; n = strlen(T); getNext(); if (n % (n - Next[n]) == 0) printf ("%d\n",n / (n - Next[n])); else printf ("1\n"); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- JavaScriptDOM基础
- 拉格朗日乘子法(Lagrange Multiplier)和KKT条件
- C语言比较两数大小
- LeetCode15. 3Sum
- HDU 1026 Ignatius and the Princess I
- POJ
- HDOJ2665 &&poj2104 k-th number(主席树+求第K小)
- 【秋招总结】我的数据分析之路已经结束了,或者说,后会有期了
- java配置环境变量教程
- EnumProcessModulesEx return false
- CSS 颜色代码大全
- Codeforces Round #149 (Div. 2) E. XOR on Segment(21棵线段树处理每一位+区间异或)
- 深入理解Java虚拟机之内存详解
- Bomb(HDU 3555 数位DP)