UVA 10100-longest Match (DP longest common sub sequence)

Source: Internet
Author: User

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)

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.