Longest Match
A newly opened detective agency is struggling with their limited intelligence to find out a secret
Information passing technique among its detectives. Since They is new in this profession, they know
Well that their messages would easily be trapped and hence modified by other groups. They want to guess
The intensions of other groups by checking the changed sections of messages. First they has the to get the
Length of longest match. You is going to the help them.
Input
The input Le may contain multiple test cases. Each case would contain the successive lines of string.
Blank lines and Non-letter printable punctuation characters may appear. Each line of string would be
No longer than characters. Length of each word would be is less than characters.
Output
For each case of input, you have to output a line starting with the case number right justified in a field
Width of, followed by the longest match as shown in the sample output. In case of at least one blank
Line for each input output '
blank!
Consider the Non-letter punctuation characters as white-spaces.
Sampleinput
This is a test.
Test
Hello!
The document provides late-breaking information
Late breaking.
Sampleoutput
1. Length of Longest match:1
2. blank!
3. Length of Longest match:2
The problem is the longest common sub-sequence of deformation.
Recursive formula for the longest common subsequence:
Dp[i+1][j+1] = {
Max (dp[i][j]+1, d[i][j+1], D[i+1][j]) (si+1 = tj+1)
Max (d[i][j+1], d[i+1][j]) other
}
#include <cstdio>#include<iostream>#include<cstring>#include<cstring>#include<cctype>using namespacestd;Const intMAXN =1003;stringTEXT[MAXN], PATTERN[MAXN];intDP[MAXN][MAXN];BOOLIsTrueCharc) { if(c >='a'&& C <='Z')|| (c >='A'&& C <='Z') || (c >='0'&& C <='9')){ return true; } return false;}voidDevide (stringStr[],stringSint&ans) {ans=0; str[0].clear (); for(inti =0; I < s.size (); i++){ if(IsTrue (S[i])) {Str[ans]+=S[i]; }Else { if(Str[ans].empty ()) {Continue; } ans++; Str[ans].clear (); } } if(IsTrue (S[s.size ()-1])) ans++;}intMain () {stringstr1, str2; intnum =0, NUM1, num2;; while(!cin.eof ()) {getline (cin, STR1); Getline (CIN, STR2); printf ("%2d.", ++num); if(Str1.empty () | |Str2.empty ()) {cout<<"blank!"<<Endl; }Else{devide (text, str1, NUM1); Devide (Pattern, str2, num2); Memset (DP,0,sizeof(DP)); for(inti =0; i < NUM1; i++){ for(intj =0; J < num2; J + +){ if(Text[i] = =Pattern[j]) {Dp[i+1][j+1] = Dp[i][j] +1; }Else{dp[i+1][j+1] = max (dp[i+1][J], dp[i][j+1]); } }} cout<<"Length of longest match:"<<DP[NUM1][NUM2] <<Endl; } } return 0;}
View Code
UVA 10100Longest Match