链接:

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;
}

最新文章

  1. ASP.NET SignalR入门
  2. 【转】document.cookie详解
  3. 第二次团队作业——预则立&amp;&amp;他山之石
  4. eclipse连hadoop2.x运行wordcount 转载
  5. NOIP2011提高组 聪明的质监员 -SilverN
  6. postgresql 关闭自动提交
  7. 第二百八十九天 how can I 坚持
  8. 决策树之ID3,C4.5及CART
  9. jsp (1)
  10. web页面中快速找到html对应元素两种方法
  11. Chargen UDP服务远程拒绝服务攻击漏洞修复教程
  12. vue数据请求显示loading图
  13. MATLAB System Generator初识
  14. jQuery调用Asp.Net后台方法
  15. Solr学习之五
  16. 20145325张梓靖 实验四 &quot;Andoid开发基础&quot;
  17. Eclipse添加中文javadoc
  18. php 操作 oracle lob 数据
  19. python基础之3
  20. python 数据类型 变量 注释

热门文章

  1. 使用Docker Maven 插件进行镜像的创建以及上传至私服
  2. 小菜鸟之HTML第一课
  3. linux系统redis安装及使用
  4. Springboot使用javaMail进行邮件发送
  5. C++入门基础知识(一)
  6. PowerBI 实现不同角色看到内容不同支持动态权限管理
  7. nodejs和npm
  8. Navicat连接CentOS7中的MariaDB
  9. 浅读vue-router源码,了解vue-router基本原理
  10. 解决Eclipse中springBoot中文乱码问题