标签:exce mit com blank des style cert try names
Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 18043 | Accepted: 8742 |
Description
Input
Output
Sample Input
3 aaa 12 aabaabaabaab 0
Sample Output
Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4
#include<cstdio> #include<cstring> using namespace std; char s[1000001]; int ne[1000001]; int main(){int i,j,n,t=0,move; while(scanf("%d",&n)&&n){memset(ne,0,sizeof(ne)); scanf("%s",s+1);printf("Test case #%d\n",++t);j=0; printf("ne:0,i:1\n"); for(int i=2,j=0;i<=n;i++){ while(s[i]!=s[j+1]&&j>0)j=ne[j]; if(s[i]==s[j+1])ne[i]=++j; //printf("ne:%d,i:%d\n",ne[i],i);----------------if you really belive the sience //you can have a try last_line_test //-------------above want ne_map //The meaning of ne_map //ne[i] mark from 1 to i‘s word,prefix and suffix has the longest_same_length //for example //aba ,prefix and suffix both have a,the longest_same_length is 1 //abab,prefix and suffix both have ab,the longest_same_length is 2 //maybe you want to ask :what is prefix,what is suffix //prefix is it has group_case except last letter //suffix is it has group_case except first letter //for example ,a word abcba //it‘s prefix is a,ab,abc,abcb //it‘s suffix is a,ba,cba,bcba //and they have one case in same(a) ,so their ne_num is 1 move=i-ne[i]; //------------------------------------------------------- // KMP algorithm use move to move one string,and check this string with another string //it‘s easier and quicker to find out coincidence(just move more quickly) //if you have not learnt KMP,I think you had better learn it //it‘s really a fast and convinient algorithm //------------------------------------------------------- if(ne[i]&&(i%move==0)) printf("%d %d\n",i,i/move); //if it has same_length|and|have repetend, //just printf the ask i and the repetend_length //you may wonder why if i%move==0 this part have repetend //let me tell you //as we know the repetend_part are always even number //for example:aabaab //the ne_num:010123 //the move_num:113333 //think it over on the if_step then run the program with the data_given //then you will find that is true //certify: //to prove this is true ,you should start with the word //for example: //aa //it‘s ne_num is 1(a) //cut it //then the word become a //abab //it‘s ne_num is 2(ab) //cut it //then the word become ab //ababab //it‘s ne_num is 4(abab) //cut it //then the word become ab //abcabc //it‘s ne_num is 3(abc) //cut it //then the word become abc //abba //it‘s ne_num is 1(a) //cut it //then the word become abb //as we see,a repetend_word has a repetend_word_part //after cut,the repetend_word always can % cut_part==0 //because except the repetend_word ,it has no other part //certify over!!!!! }printf("\n"); }return 0; }
program is just above.
标签:exce mit com blank des style cert try names
原文地址:http://www.cnblogs.com/muzu/p/7141931.html