D-Nana in Wonderland Series--The villagers ' quirks

Source: Internet
Author: User

D-Nana in Wonderland Series--The villagers ' quirksTime Limit:2000/1000ms (java/others) Memory Limit: 128000/64000kb (java/others) Submit Statusproblem Description

Nana laborious Dickens finally put the candy Eat finished (agreed to eat not finish? Lie, Mouth heng ~), so, Edge Brook Line, forget road of distance. Suddenly every peach blossom forest, clip shore Hundreds of steps, no miscellaneous tree, grass delicious, lost its blossom, Nana very different. Go forward, to the poor forest. Forest to the water, then a mountain, the mountain has a small mouth, as if there is light. Then the boat, from the mouth into. The beginning of the very narrow, just pass people. Re-line dozens of steps, enlightened. Land Groud, houses, there is fertile farmland beauty Chisang of the genus. Criss traffic, consensual. Among them, men and women dress, as outsiders. HUANGFA, and impoverished. (Excerpt from the Peach Blossom Garden)

Nana and the villagers for a long time to find the people here to the name of the child is very strange, first of all the villagers are the name of a special symbol to record, is exactly 26 symbols, so Nana will replace them with ' a ' ~ ' Z ', and then the first father's name as the child's surname, mother's name as the child's name. At this time certainly someone will ask, not only child what to do? Very simple ~ Take the name of the stitching good prefix and suffix the same part from short to long as the child's name, what, not enough? Then you are not allowed to regenerate the child!

However, because the peach blossom villagers have been isolated for so long that they are always unable to correctly judge how many children a couple can have, they give you the task.

P.S. If you use S[1,n] to represent a string of length n, then S[1,i] (1<=i<=n) represents a prefix of S, S[j,n] (1<=j<=n) represents a suffix of s. Look at the sample explanation in detail

Input

Multiple sets of data, first a positive integer t (t<=20), representing the number of groups of data

For each set of data, there are two string s1,s2 that consist of only ' a ' ~ ' Z ' without spaces, representing the husband's name and the wife's name, respectively. (1<=| s1|,| s2|<=100000)

Output for each set of data, outputs an integer indicating the maximum number of children the couple can have. Sample Input
2ABABC ababaaaaaa AAA
Sample Output
38
Hint

For example 1, the name of the husband and wife is stitched together is Ababcababa, can be the name of the child is a, ABA, Ababcababa, so the maximum birth of 3 children

For example 2, the name of the husband and wife together is aaaaaaaa, can be the name of the child is a, AA, AAA, AAAA, AAAAA, aaaaaa, AAAAAAA, aaaaaaaa, so the maximum birth of 8 children

Solution: This topic is to ask, for the new composition of the string, how many of the same prefix with Str[0] as the starting point and thesuffix of str [N-1], The method is to use the next array, because the next array is stored in the number is next[ I] is the maximum length that represents the prefix with Str[0] as the starting point and the suffix with str[i-1] as the end point. According to test instructions, you only need to find the next next[k from Next[n]=k, start looking for the same prefix with the maximum length, find the Next[j]==-1, (find the first character) until you find how many times you need to find to jump to the first character, output this number is the required answer 、

1#include <iostream>2#include <stdio.h>3#include <string.h>4 using namespacestd;5 Charstr[300000];6 Charstr_s[300000];7 voidGetNext (CharP[],intNext[],intlen_t)/*Next Array,*/8 {9     intI=0, j=-1;Tennext[0]=-1;/*start Assignment is-1*/ One      while(i<len_t) A     { -         if(j==-1|| P[I]==P[J]) next[++i]=++J; -         Elsej=Next[j]; the     } - } - intMain () - { +     intnext[200005]; -     intt,i,len,sum,j,sign; +     Charstr; Ascanf"%d",&T); GetChar (); at      while(t--) -     { -          for(i=0;; i++) -         { -Str=GetChar (); -             if(str== +) i--; in             Else if(str=='\ n') {str[i]=' /'; Len=i; Break;} -             Elsestr[i]=str; to         } +sum=0; -j=Len; theGetNext (Str,next,len);/*the string of the composition to find Next array*/ *          while(next[j]!=-1) $         {Panax NotoginsengJ=NEXT[J];/*For details, see next[i] The principle of an array, *next[i] The maximum length of the same prefix that is stored in the I position,*/ -sum++;/*to recursively find the number in next, it takes a few moments to jump to the starting point to find out how many of the same prefixes*/ the         } +printf"%d\n", sum); A     } the     return 0; +}
View Code

D-Nana in Wonderland Series--The villagers ' quirks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.