Cyclic Nacklace

Problem Description
CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspired by the entrepreneurial spirit of "HDU CakeMan", he wants to sell some little things to make money. Of course, this is not an easy task.

As Christmas is around the corner, Boys
are busy in choosing christmas presents to send to their girlfriends.
It is believed that chain bracelet is a good choice. However, Things are
not always so simple, as is known to everyone, girl's fond of the
colorful decoration to make bracelet appears vivid and lively, meanwhile
they want to display their mature side as college students. after CC
understands the girls demands, he intends to sell the chain bracelet
called CharmBracelet. The CharmBracelet is made up with colorful pearls
to show girls' lively, and the most important thing is that it must be
connected by a cyclic chain which means the color of pearls are cyclic
connected from the left to right. And the cyclic count must be more than
one. If you connect the leftmost pearl and the rightmost pearl of such
chain, you can make a CharmBracelet. Just like the pictrue below, this
CharmBracelet's cycle is 9 and its cyclic count is 2:

Now
CC has brought in some ordinary bracelet chains, he wants to buy
minimum number of pearls to make CharmBracelets so that he can save more
money. but when remaking the bracelet, he can only add color pearls to
the left end and right end of the chain, that is to say, adding to the
middle is forbidden.
CC is satisfied with his ideas and ask you for help.

 
Input
The first line of the input is a single integer T ( 0 < T <= 100 ) which means the number of test cases.
Each
test case contains only one line describe the original ordinary chain
to be remade. Each character in the string stands for one pearl and
there are 26 kinds of pearls being described by 'a' ~'z' characters. The
length of the string Len: ( 3 <= Len <= 100000 ).
 
Output
For each case, you are required to output the minimum count of pearls added to make a CharmBracelet.
 
Sample Input
3
aaa
abca
abcde
 
Sample Output
0
2
5
 
Author
possessor WC
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3336 1358 1686 2222 2896
 
题目大意:给一串字符串,问至少添加多少个字符,使得这个字符串有至少两个循环节

符合 i % ( i - next[i] ) == 0 && next[i] != 0 , 则说明字符串循环,而且

循环节长度为:   i - next[i]
循环次数为:       i / ( i - next[i] )

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
using namespace std;
const int N = ;
int c[N] ;
int n ;
//int p[220009] ;
char a[] ;
char b[]; void getnext(char *b , int *next)
{
int len = strlen(b);
next[] = - ;
int k = - , j = ;
while(j < len)
{
if(k == - || b[j] == b[k])
{
k++;
j++;
if(b[j] != b[k])
next[j] = k;
else
{
next[j] = next[k];
}
}
else
k = next[k];
}
} int main()
{
int n ;
cin >> n ;
while(n--)
{
int next[];
scanf("%s" , a);
int len = strlen(a);
getnext(a , next);
int l = len - next[len];
int ans = ;
if(len != l && len % l == )
{
printf("0\n");
}
else
{
// cout << l << " " << next[len] <<endl ;
ans = l - len%l ;
printf("%d\n" , ans);
}
} return ;
}

kmp(循环节)的更多相关文章

  1. poj 2406 Power Srings (kmp循环节) (经典)

    <题目链接> 题目大意: 给出一个字符串,求其字串在该字符串中循环的最大周期. 解题分析: length=len-Next[len],len为该字符串的最小循环节,如果len%length ...

  2. Period(KMP,循环节问题)

    题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...

  3. FZU 1901 Period II(KMP循环节+公共前后缀)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 题目大意:题目大意求出所有p满足s[i]=s[i+p](i<=len-p) 解题思路: 其实就是要 ...

  4. POJ 2185 Milking Grid KMP循环节周期

    题目来源:id=2185" target="_blank">POJ 2185 Milking Grid 题意:至少要多少大的子矩阵 能够覆盖全图 比如例子 能够用一 ...

  5. [KMP求最小循环节][HDU3746][Cyclic Nacklace]

    题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...

  6. [KMP求最小循环节][HDU1358][Period]

    题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...

  7. hdu 4333"Revolving Digits"(KMP求字符串最小循环节+拓展KMP)

    传送门 题意: 此题意很好理解,便不在此赘述: 题解: 解题思路:KMP求字符串最小循环节+拓展KMP ①首先,根据KMP求字符串最小循环节的算法求出字符串s的最小循环节的长度,记为 k: ②根据拓展 ...

  8. hust 1010 The Minimum Length(循环节)【KMP】

    <题目链接> 题目大意: 有一个字符串A,一次次的重写A,会得到一个新的字符串AAAAAAAA.....,现在将这个字符串从中切去一部分得到一个字符串B,例如有一个字符串A="a ...

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

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

  10. HDU 1358 Period(KMP+最小循环节)题解

    思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...

随机推荐

  1. append动态生成的元素,无法触发事件的原因及解决方案

    今天笔者在实现一个简单的动态生成元素功能的时候,发现了一个问题: 使用append动态生成的元素事件绑定失效了. 查阅资料后发现: click(fn)当选中的选择器被点击时触发回调函数fn.只针对与页 ...

  2. MongoDB的使用学习之(二)简介

    原文链接:http://www.cnblogs.com/yxlblogs/p/3681089.html MongoDB 是一个高性能,开源,无模式的文档型数据库,是当前 NoSQL 数据库产品中最热门 ...

  3. select 项目<选课系统>

    """1. 创建北京.上海 2 所学校 学校类对象 属性:名字,地址 行为: 2. 创建linux , python , go 3个课程 , linux\py 在北京开, ...

  4. 关于webpack高版本向低版本切换 如何切换?

    卸载:npm uninstall webpack -g 重新安装:npm install webpack@3.7.1 -g 直接安装指定版本就行了,如安装 2.4.1 版:cnpm install w ...

  5. 网络拓扑_配置hybrid端口

    目的:实现不同VLAN间的PC不可互访, 但不同VLAN的PC均可以访问服务器. 例: VLAN5与VLAN10的PC不可以互通,但它们均可与服务器VLAN50互通. 拓扑图:

  6. python笔记(1)--基础知识

    一.注释 单行注释 #打印“hello world” print("hello.world!") 另外一种单行注释 print("hello,world!") ...

  7. 【leetcode】912. Sort an Array

    题目如下: Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1 ...

  8. python实现Restful服务 (基于flask)(1)

    参考:https://www.jianshu.com/p/6ac1cab17929 参考:https://www.cnblogs.com/alexyuyu/p/6243362.html 参考:http ...

  9. 使用WebAPI流式传输大文件(在IIS上大于2GB)

    这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...

  10. igserver许可证过期 & 发布服务客户端浏览出现error2032

    1.卸载igserver重装 卸载包IGServerForDotNet成功! 卸载包IGServerCore成功! 卸载包MiddleWare_ArcGIS成功! 卸载包SDE_DM成功! 卸载包SD ...