Cyclic Nacklace 杭电3746
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.
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 ).
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=1E6+;
char arr[N];
ll nxt[N];
//abcdefabcdefab
//总长度n减去nxt[n-1]就是循环节的最短长度
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%s",arr);
ll n=strlen(arr);
for(ll i=;i<n;i++){
ll j=nxt[i-];
while(arr[i]!=arr[j]&&j>) j=nxt[j-];
if(arr[i]==arr[j]) j++;
nxt[i]=j;
}
int len=n-nxt[n-];
if(len==n) printf("%d\n",len);
else if(n%len==){
puts("");
}
else printf("%d\n",len-n%len);
}
return ;
}
Cyclic Nacklace 杭电3746的更多相关文章
- (KMP扩展 利用循环节来计算) Cyclic Nacklace -- hdu -- 3746
http://acm.hdu.edu.cn/showproblem.php?pid=3746 Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others ...
- 模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace
Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽 ...
- Cyclic Nacklace HDU 3746 KMP 循环节
Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...
- HDU 3746:Cyclic Nacklace
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 3746 Cyclic Nacklace KMP循环节
Cyclic Nacklace 题意:给一个长度为Len( 3 <= Len <= 100000 )的英文串,问你在字符串后面最少添加几个字符可以使得添加后的串为周期串? Sample I ...
- hdoj 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(KMP循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 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(求补齐循环节最小长度 KMP中next数组的使用 好题!!!)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- 文件解析漏洞总结(IIS,APACHE,NGINX)
(本文主体来自https://blog.csdn.net/qq_36119192/article/details/82834063) 文件解析漏洞 文件解析漏洞主要由于网站管理员操作不当或者 Web ...
- 推荐系统(Recommender Systems)
本博客是针对Andrew Ng在Coursera上的machine learning课程的学习笔记. 目录 基于内容的推荐(Content-based recommendation) 问题表述 问题范 ...
- Python电影数据分析
数据说明:MovieLens数据集,它包含来自于943个用户以及精选的1682部电影的100K个电影打分.每个用户至少为20部电影打分,数据类型user id | item id | rating | ...
- 强化学习之五:基于模型的强化学习(Model-based RL)
本文是对Arthur Juliani在Medium平台发布的强化学习系列教程的个人中文翻译,该翻译是基于个人分享知识的目的进行的,欢迎交流!(This article is my personal t ...
- DrQA 阅读维基百科来回答开放问题 Reading Wikipedia to Answer Open-Domain Questions
DrQA 是一个阅读理解系统用在开放领域问答.特别的,DrQA 针对一个机器阅读任务.在这个列表里,我们为一个潜在非常大的预料库中搜索一个问题的答案.所以,这个系统必须结合文本检索和机器文本理解. 项 ...
- 关于getchar的疑惑
最近做了一道题,我的代码有片段是这样的 while(scanf("%d",&n)) { if(n==0&&getchar()=='\n') break; . ...
- 20175314 实验五 Java网络编程
20175314 实验五 Java网络编程 一.实验报告封面 课程:Java程序设计 班级:1753班 姓名:薛勐 学号:20175314 指导教师:娄嘉鹏 实验日期:2018年5月31日 实验时间: ...
- 通用mapper常用注解
通用mapper的作用: 自动实现单表的增删改查 常用注解使用 @Table 作用:建立实体类和数据库表之间的对应关系. 默认规则:实体类类名首字母小写作为表名.Employee 类→employee ...
- Android学习----自定义Adapter实现ListView
前言: 对于ListView而言,自定义的Adapter对于显示复杂的界面有很大的灵活性 .使用自定义的Adapter需要继承BaseAdapter,然后重写getCount(),getView(), ...
- 除了chrome、Firefox之外其他浏览器全都连不上网
在调试jsp时,总是会遇到eclipse打开jsp网页失败,没有网络,浏览器也除了chrome.Firefox之外其他浏览器全都连不上网,这里我也不清楚是什么问题,但是解决方法是: 打开Interne ...