标签:continue oid send chain map ext most case check
InputThe 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 ).OutputFor 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
题意:给一个字符串你,求需要在末尾加几个字符使它变成最少两个循环的
题解:利用Next数组的性质,循环节就是slen-Next【slen-1】-1,刚开始自己推出来了,结果细节没考虑到):
#include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<iomanip> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pi acos(-1) #define ll long long #define mod 1000000007 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-9; const int N=100000+5,maxn=1000000+5,inf=0x3f3f3f3f; int Next[N],slen,plen; string str,ptr; void getnext() { int k=-1; Next[0]=-1; for(int i=1;i<slen;i++) { while(k>-1&&str[k+1]!=str[i])k=Next[k]; if(str[k+1]==str[i])k++; Next[i]=k; } } int main() { ios::sync_with_stdio(false); cin.tie(0); // cout<<setiosflags(ios::fixed)<<setprecision(2); int t; cin>>t; while(t--){ cin>>str; slen=str.size(); getnext(); if(Next[slen-1]==-1) { cout<<slen<<endl; continue; } int ans=slen-Next[slen-1]-1; if(slen%ans==0)cout<<0<<endl; else cout<<ans-slen%ans<<endl; } return 0; }
标签:continue oid send chain map ext most case check
原文地址:http://www.cnblogs.com/acjiumeng/p/6808931.html