Cyclic Nacklace
来源:互联网 发布:数据聚合是什么意思 编辑:程序博客网 时间:2024/06/01 08:20
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.
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 ).
3aaaabcaabcde
025
题意:现在有给定的一个字符串,目标是使得头尾相接后的字符环由2个或者2个以上的相同字符串(基串)连接而成。问最少需要加多少字符。(字符只能加在头或尾)
题解:因为练的是拓展kmp的专题,所以就往拓展kmp上想,于是跑拓展kmp,跑完后得到nt[]数组。在本子上画画发现当nt[i]==len时,如果此时还满足nt[i]%i==0的话,会发现这个字符串是由前面i+1(这里i从0开始计算)个字符重复得到,答案直接为0;如果不满足nt[i]%i==0的话,则剩下的nt[i]%i的几个字符缺了几个字符凑成i个就能满足题意要求,于是答案就是i-nt[i]%i。综合两种情况,直接得到答案为(i-nt[i]%i)%i,找最小的i就行了;
代码:
#include <cstdio>#include <cstdlib>#include <iostream>#include <cstring>#include <string>#include <cstdlib>#include <algorithm>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <cmath>#include <ctime>using namespace std;typedef long long ll;typedef pair<int, int> P;const int INF = 0x3f3f3f3f;const ll LINF = 0x3f3f3f3f3f3f3f3f;const double PI = acos(-1.0);const double eps = 1e-10;const int maxn = 1e5+7;const int mod = 10007;int t;char s[maxn];int nt[maxn];void solve(){ memset(nt,0,sizeof(0)); int len = strlen(s); int i = 0,po = 1; nt[0] = len; while(i+1<len&&s[i]==s[i+1]) i++; nt[1] = i; for(int i = 2;i < len;i++){ if(nt[i-po]+i<nt[po]+po) nt[i] = nt[i-po]; else { int j = nt[po]+po-i; if(j<0) j = 0; while(j+i<len&&s[j+i]==s[j]) j++; nt[i] = j; po = i; } } //for(int i = 0;i < len;i++) cout<<nt[i]<<" "; for(int i = 1;i < len;i++){ if(nt[i]+i==len){ printf("%d\n",(i-nt[i]%i)%i); return; } } printf("%d\n",len);}int main(){ scanf("%d",&t); while(t--){ scanf("%s",s); solve(); } return 0;}
阅读全文
1 0
- Cyclic Nacklace
- Cyclic Nacklace
- Cyclic Nacklace
- Cyclic Nacklace
- Cyclic Nacklace
- Cyclic Nacklace
- Cyclic Nacklace
- Cyclic Nacklace
- hdu3746 Cyclic Nacklace
- HDU-3746 Cyclic Nacklace
- HDU 3746 Cyclic Nacklace
- hdoj 3746 Cyclic Nacklace
- 【KMP】 hdu3746 Cyclic Nacklace
- hdu 3746 Cyclic Nacklace
- HDU3746:Cyclic Nacklace
- hdu 3746 Cyclic Nacklace
- HDU 3746 Cyclic Nacklace
- hdu3476-Cyclic Nacklace
- Facebook 发布黑科技:用大脑直接「打字」,让皮肤去「倾听」| F8 2017
- 梯度下降
- 隐藏软键盘的时候,页面或者布局往上跳动的解决办法
- 活动——活动的启动模式
- 在一台Linux上部署多个tomcat
- Cyclic Nacklace
- 易到三位联合创始人周航、杨芸与汤鹏宣布辞职
- 任正非内部讲话:华为会不会是下一个美联航?
- 马斯克最疯狂的公司Neurallink,它要定义未来人类
- php中的echo print print_r的区别
- 小程序入口再扩大:公众号可以关联不同主体的小程序
- Reflection2017.10.27
- easyui 部分常用控件的取值和赋值
- 外媒:与苹果撕破脸,高通基带订单或大减