hdu 3746 Cyclic Nacklace(KMP)
题意:
求最少需要在后面补几个字符能凑成两个循环。
分析:
最小循环节的应用,i-next[i]为最小循环节。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define N 100010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int f[N];
char str[N];
void getnext(int n){
int i=,j=-;
f[]=-;
while(i<n){
if(j==-||str[i]==str[j]){
i++;
j++;
f[i]=j;
}
else j=f[j];
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%s",str);
int len=strlen(str);
getnext(len);
int tmp=len-f[len];
if(tmp==len)
printf("%d\n",len);
else if(len%tmp==)
printf("0\n");
else
printf("%d\n",tmp-len%tmp);
}
return ;
}
hdu 3746 Cyclic Nacklace(KMP)的更多相关文章
- HDU 3746 Cyclic Nacklace(KMP找循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 题目大意:给你一个字符串,求出将字符串的最少出现两次循环节需要添加的字符数. 解题思路: 这题需 ...
- HDU 3746 Cyclic Nacklace(KMP+最小循环节)题解
思路: 最小循环节的解释在这里,有人证明了那么就很好计算了 之前对KMP了解不是很深啊,就很容易做错,特别是对fail的理解 注意一下这里getFail的不同含义 代码: #include<io ...
- HDU 3746 Cyclic Nacklace (用kmp求循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3746 Cyclic Nacklace(next数组求最小循环节)
题意:给出一串字符串,可以在字符串的开头的结尾添加字符,求添加最少的字符,使这个字符串是循环的(例如:abcab 在结尾添加1个c变为 abcabc 既可). 思路:求出最小循环节,看总长能不能整除. ...
- HDU 3746:Cyclic Nacklace(KMP循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace
Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...
- HDU 3746 Cyclic Nacklace(kmp next数组运用)
Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...
- HDU 3746 Cyclic Nacklace(求补齐循环节最小长度 KMP中next数组的使用 好题!!!)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- HDU 3746 Cyclic Nacklace (KMP找循环节)
题目链接:HDU 3746 Sample Input 3 aaa abca abcde Sample Output 0 2 5 Author possessor WC Source HDU 3rd & ...
随机推荐
- PHP输入流php://input [转]
对于php://input介绍,PHP官方手册文档有一段话对它进行了很明确地概述. "php://input allows you to read raw POST data. It is ...
- Linux多线程编程和Linux 2.6下的NPTL
Linux多线程编程和Linux 2.6下的NPTL 在Linux 上,从内核角度而言,基本没有什么线程和进程的区别--大家都是进程.一个进程的多个线程只是多个特殊的进程他们虽然有各自的进程描述结构, ...
- SRAM与SDRAM的区别
http://www.cnblogs.com/spartan/archive/2011/05/06/2038747.html SDRAM SDRAM(Synchronous Dynamic Rando ...
- ASP.NET并发处理
http://blog.csdn.net/hliq5399/article/details/6280288
- iOS开发--即时通讯
什么是环信? 1.环信是一个第三平台,提供即时通信(IM–Instant Messaging )的服务 2.环信是在XMPP的基础上进行二次开发 3.环信在网络上传输的数据也是XML 4.使用环信,不 ...
- 开源入侵检测系统OSSEC搭建之一:服务端安装
OSSEC是一款开源的多平台的入侵检测系统,可以运行于Windows, Linux, OpenBSD/FreeBSD, 以及 MacOS等操作系统中.主要功能有日志分析.完整性检查.rootkit检测 ...
- AXURE制作APP抽屉式菜单
1.拖一个dynamic panel到窗体,将State1改名为State_首页: 2.拖2个dynamic panel到State_首页中,分别命名为侧边菜单及首页内容,首页内容盖住侧边菜单: 3. ...
- ios ableviewcell的动态加载数据,模仿喜马拉雅动态数据加载
iphone(UITableViewCell)动态加载图片http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Intr ...
- Android开发之获取系统管理权限,即DevicePolicyManager和DeviceAdminReceiver的使用
参考:http://www.cnblogs.com/androidez/archive/2013/02/17/2915020.html 1.创建AdminReceiver,继承DeviceAdminR ...
- (六)Ireport制作一个规范的报表,处理数据格式
转载:http://frankco.iteye.com/blog/1686651 删除注释信息,Report Respector面板中按住Ctrl鼠标选中位于报表每个部分的组件,使用键盘的方向键可以左 ...