HDU-3746-Cyclic nacklace(KMP, 循环节)
2024-10-06 21:53:07
链接:
https://vjudge.net/problem/HDU-3746
题意:
第一题来啦。
现在给你一个字符串,请问在该字符串末尾最少添加多少个字符,可以让这个字符串获得重复循环序列。
思路:
考虑一个用字符串长度为len, 并且是由长度为l的子串循环组成.
我们有S[0,len-l-1] = S[l, len], 在循环次数大于2的时候,
所以len - Next[len] 就是最小循环节的长度.
画画图就好懂了.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e6+10;
int Next[MAXN];
string s, p;
void GetNext()
{
int len = p.length();
Next[0] = -1;
int j = 0;
int k = -1;
while (j < len)
{
if (k == -1 || p[k] == p[j])
{
++k;
++j;
Next[j] = k;
}
else
k = Next[k];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--)
{
cin >> p;
GetNext();
int len = p.length()-Next[p.length()];
if (len != p.length() && p.length()%len == 0)
cout << 0 << endl;
else
cout << len-(p.length()%len) << endl;
}
return 0;
}
最新文章
- ASP.NET SignalR入门
- 【转】document.cookie详解
- 第二次团队作业——预则立&;&;他山之石
- eclipse连hadoop2.x运行wordcount 转载
- NOIP2011提高组 聪明的质监员 -SilverN
- postgresql 关闭自动提交
- 第二百八十九天 how can I 坚持
- 决策树之ID3,C4.5及CART
- jsp (1)
- web页面中快速找到html对应元素两种方法
- Chargen UDP服务远程拒绝服务攻击漏洞修复教程
- vue数据请求显示loading图
- MATLAB System Generator初识
- jQuery调用Asp.Net后台方法
- Solr学习之五
- 20145325张梓靖 实验四 ";Andoid开发基础";
- Eclipse添加中文javadoc
- php 操作 oracle lob 数据
- python基础之3
- python 数据类型 变量 注释