一、题目

  HDU3746

二、分析

  KMP比较好解决的一个问题:如果求一个串中的循环节?

  仔细回想KMP的用法,重点是next数组,相当于就是后缀和前缀的比较,那么不正是方便了我们确定循环节?

  如果以字符串的最后一个位置(非字符)分析,那么这个位置的当前next值,就是我们串前缀和后缀的交集的最长值,所以只需要用长度Len去减去这个next值就可以得出循环节的长度了。

  1 $Len\%(Len-next[Len]) \&\& Len != Len - next[Len]$

    此时,字符串已经满足循环的要求。

  2 上面条件的反

    此时,要求后面需要补多少字符串,答案就是$ (Len - next[Len]) - Len\%(Len-next[Len]) $ 其实就是循环节长度减去最后一未补齐的长度。

三、AC代码

 1 #include <cstdio>
2 #include <iostream>
3 #include <cstring>
4 using namespace std;
5 const int maxn = 1e5 + 14;
6 char s[maxn];
7 int Next[maxn], Len;
8
9 void get_next()
10 {
11 Next[0] = -1;
12 int i = 0, j = -1;
13 while(i <= Len)
14 {
15 if(j == -1 || s[i] == s[j])
16 {
17 i++;
18 j++;
19 Next[i] = j;
20 }
21 else
22 {
23 j = Next[j];
24 }
25 }
26 }
27
28 int solve()
29 {
30 get_next();
31 int ans = Len - Next[Len];
32 if(Len % ans == 0 && Len != ans)
33 return 0;
34 else
35 return ans - (Len % ans);
36 }
37
38 int main()
39 {
40 //freopen("input.txt", "r", stdin);
41 int T;
42 scanf("%d", &T);
43 while(T--)
44 {
45 scanf("%s", s);
46 Len = strlen(s);
47 printf("%d\n", solve());
48 }
49 return 0;
50 }

HDU_3746 Cyclic Nacklace 【KMP的应用】的更多相关文章

  1. HDU_3746 Cyclic Nacklace(KMP)

    题目请点我 题解: 题目大意:有一个字符串s.能够在前后加入字符,使字符串成为一个循环次数至少为2的循环字符串.输出最少须要加入的字符数目. 首先能够证明题目能够等价为仅仅在末尾加入字符使满足题意.要 ...

  2. HDU 3746 Cyclic Nacklace(kmp next数组运用)

    Cyclic Nacklace Problem Description CC always becomes very depressed at the end of this month, he ha ...

  3. hdu 3746 Cyclic Nacklace KMP循环节

    Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...

  4. HDU3746 Cyclic Nacklace —— KMP 最小循环节

    题目链接:https://vjudge.net/problem/HDU-3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    M ...

  5. hdu3746 Cyclic Nacklace KMP

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

  6. Match:Cyclic Nacklace(KMP的next数组的高级应用)(HDU 3746)

    串珠子 题目大意:给定一个字串,要你找到如果要使之成为循环串,在末尾需要的最小的字数(只能添加字符,不能删减字符) 首先联动一下之前做过的动态规划问题POJ 3280,当然了3280这一题是用的LD, ...

  7. HDU 3746 Cyclic Nacklace KMP

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 KMP算法—— AC代码: #include <iostream> #include ...

  8. hdu 3746 Cyclic Nacklace(kmp最小循环节)

    Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...

  9. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

随机推荐

  1. 攻防世界-Web-lottery(.git泄露、php源码审计、弱类型利用)

    扫描目录,发现.git泄露: 提取.git泄露的源码,得到许多文件: 网站这里: 这就要审计一下代码,找找漏洞了. 经过一番审计,猜数字对应的函数在api.php中: 我们要绕过这个$win_numb ...

  2. 指纹采集器Live 20R

    最近有个项目需要使用指纹采集器Live 20R,买来这个小玩意后不知道怎么用,看了一些教程和自己摸索了一下,才初步掌握了用的方法. 环境: 硬件:联想 小新 操作系统:Win 10 IDE:VS201 ...

  3. [转]C# web 读取Excel文件

    项目中总是遇到要整理基础数据的问题,少量的数据还好说,如果数据量大的话,这无疑会增加项目开发的用时,拖延交期. 那么我们会让客户自己去整理基础数据,但是问题是,客户整理的数据怎写入系统呢?我们一般会采 ...

  4. Ant-design-vue—— 表单输入框输入很卡问题

    参考:https://blog.csdn.net/weixin_43905402/article/details/106074435 我的问题:vue项目中使用ant-design-vue,表单中输入 ...

  5. Free Video Player All In One

    Free Video Player All In One VLC media player https://github.com/videolan/vlc VideoLAN https://www.v ...

  6. how to publish a UMD module

    how to publish a UMD module 如何发布UMD模块 npm https://github.com/xgqfrms/umd-npm-package https://www.npm ...

  7. web cache & web storage all in one

    web cache & web storage all in one web cache in action web cache best practices web storage in a ...

  8. react hooks useEffect 取消 promise

    react hooks useEffect 取消 promise cancel promise https://github.com/facebook/react/issues/15006#issue ...

  9. Python 爬虫使用动态切换ip防止封杀

    对于爬虫被封禁 ! 爬虫一般来说只要你的ip够多,是不容易被封的. 一些中小网站要封杀你,他的技术成本也是很高的,因为大多数网站没有vps,他们用的是虚拟空间或者是sae,bae这样的paas云. 其 ...

  10. Python基础之:数字字符串和列表

    目录 简介 数字 字符串 字符串对象str 列表 简介 Python的主要应用是进行科学计算,科学计算的基础就是数字,字符串和列表.本文将会详细的给大家介绍一下这三个数据类型的使用情况. 数字 数字是 ...