Title Address: UVA 10100
Test instructions: Find the largest number of identical words in the two sets of strings that appear sequentially.
Idea: The successive letters in the string are considered as one word, and the words in two strings are calculated sequentially, where the word sequence of the 1th string is t1.word[1].....t1.word[n], and the 2nd string is the word sequence t2.word[1].....t2.word[m]. Each word is then treated as a character, using the LCS algorithm to calculate the longest common subsequence of two strings, which is the longest match.
#include <stdio.h>#include <math.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <sstream>#include <algorithm>#include <set>#include <queue>#include <stack>#include <map>#pragma COMMENT (linker, "/stack:102400000,102400000")using namespace STD;typedef Long LongLL;Const intinf=0x3f3f3f3f;Const DoublePi=ACOs(-1.0);Const Doubleesp=1e-7;Const intmaxn=1010;intDP[MAXN][MAXN];The maximum number of words that match the first I word in//STR1 and the first J word in S2. stringSTR1,STR2;structnode{intNumstringWORD[MAXN];} T1,t2;voidDevide (stringSstructNode &t) {intLen=s.size (); t.num=1; for(intI=0;i< +; i++) t.word[i].clear (); for(intI=0; i<len;i++) {if((s[i]>=' A '&&s[i]<=' Z ')|| (s[i]>=' A '&&s[i]<=' Z ')|| (s[i]>=' 0 '&&s[i]<=' 9 ')) T.word[t.num]+=s[i];Elset.num++; }intCnt=0; for(intI=1; i<=t.num;i++)if(!t.word[i].empty ()) t.word[++cnt]=t.word[i]; t.num=cnt;}intMain () {intIcase=1; while(!Cin. EOF ()) {getline (Cin, str1); Devide (STR1,T1); GetlineCin, str2); Devide (STR2,T2);printf("%2d.", icase++);if(Str1.empty () | | Str2.empty ()) {printf("blank!\n");Continue; } for(intI=0; i<=t1.num;i++) dp[i][0]=0; for(intI=0; i<=t2.num;i++) dp[0][i]=0; for(intI=1; i<=t1.num;i++) for(intj=1; j<=t2.num;j++) {if(T1.word[i]==t2.word[j]) dp[i][j]=dp[i-1][j-1]+1;ElseDp[i][j]=max (dp[i-1][j],dp[i][j-1]); }printf("Length of longest match:%d\n", Dp[t1.num][t2.num]); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA 10100-longest Match (DP longest common sub sequence)