Tongji online judge preliminaries (2): Love Letter

Source: Internet
Author: User

Love letter

Time Limit: 1000 msMemory limit: 10000 K

 

【Description]

 

Li Ge recently received a love letter, and the sincere internal monologue and pure emotional narration Between the Lines touched Li Ge. However4I don't know why a girl suddenly writes a love letter to herself. Li Ge, who has a high IQ, quickly thought that it was possible that his friends and friends were teasing him, so he agreed on the evening of a certain month according to his love letter.12Before arriving in huangdu town, a suburb of Shanghai, Li Ge intends to secretly investigate whether the love letter was prepared by his gang of dead parties.

He spent a lot of time finally collecting the strange poems that these guys usually write to girls, their mood diaries, reviews, and so on "). Next, he intendsCompositionTo analyze whether the mysterious love letter he received came from these people. First, he wants to find the sentence that is exactly the same between the two (love letters and materials). To get more reliable analysis results, he wants to find the longest public sentence between the two. However, this is not easy, so he findsACMTraining you, hope you can writeProgramTo help him find the longest common sentence.

Here you only need to complete the core work and find the length of the longest common sentence.

 

【Input]

 

The1The row contains only one integer.T(0<T≤10), Indicating a totalTGroup test data.

From2Line3 t + 1Each row contains a group of test data. The first row of each group of test data contains two integers.M,N(0<M,N≤100), The second behavior has a lengthMIs the content of the love letter. The length of the third behavior isNIs the content of the material. To simplify processing,Both strings contain only lowercase English letters.

 

【Output]

TotalTLine, each line corresponds to a group of test data answers (IThe answer to the group test data is output inIRows ).

The answer to each group of test data is an integer: the length of the longest common sentence (number of characters ).

 

【Sample Input]

3

10 24

Helloworld

Thisisalowercasesentense

6 8

Abcdef

Ghijklmn

9 22

Abcababab

Bababcababcabababcab

【Sample output]

 

3

0

9

 

 

[Example]

 

The longest common sentence length of the first group of data is3This is becauseHelloworldAndThisisalowercasesentenseBoth strings have a common substring.Low,Its length is3And does not exist. The length exceeds3.

Second group of dataAbcdefAndGhijklmnBecause no substring exists at all, the longest length of a common sentence is0.

Group 3AbcabababAndBababcababcabababcabAll containAbcabababThe length of this substring is9And there is no longer public substring. Therefore, the answer is:9.

-----------------------------------------------------------------------

Reference answer:

# Include <stdio. h>
# Include <string. h>

Int T, prob;
Int M, N;
Char S1 [110], S2 [110];
Int C [110] [110];
Int f [110] [110];

Int LCS (int I, Int J)
{
If (C [I] [J]! = Prob ){
C [I] [J] = prob;
If (I = M | j = n | S1 [I]! = S2 [J])
F [I] [J] = 0;
Else
F [I] [J] = 1 + LCS (I + 1, J + 1 );
}

Return f [I] [J];
}

Int main ()
{
Int I, J, K, Max;

Scanf ("% d", & T );
For (prob = 1; prob <= T; prob ++ ){
Scanf ("% d", & M, & N );
Scanf ("% s", S1 );
Scanf ("% s", S2 );

Max = 0;
For (I = 0; I <m; I ++)
For (j = 0; j <n; j ++ ){
K = LCS (I, j );
If (k> MAX) max = K;
}

Printf ("% d \ n", max );
}

Return 0;
}

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.