Implementation of DP in character matching

Source: Internet
Author: User

Here, we will save the short-term DP implementation questions for character matching.

For different strings, the two can only continuously match the subscript over the next line to obtain the maximum number of matches.

For example, the maximum number of matches between ABCD and dcba can only be 1, because after two d matches, the first string cannot match with the previous character (of course, if you match a, B, c is the same)

If you want to find the matching successful character for each question, we need a function to recursively find the previous matching successful character, here we introduce a t [N] [N] flag to help us determine when to perform recursion.

All the questions here are related to this form.

1. poj 1458 http://vjudge.net/problem/viewProblem.action? Id = 17083

This is the maximum number of matching strings.

DP [I] [J] indicates the maximum number of matches obtained when the previous string takes the I bit and the last string takes the J bit.

DP equation: DP [I] [J] = {DP [I-1] [J-1] + 1, A [I] = B [J] | max (DP [I-1] [J], DP [I] [J-1])}

At the very beginning, I wrote DP [I] [J] = {DP [I-1] [J-1] + 1, A [I] = B [J] | max (DP [I] [J], DP [I-1] [J-1])}

It is easy to find that DP [I] [J] = max (DP [I] [J], DP [I-1] [J-1]) is just a part of the state, in addition, DP [I] [J] only appears once in I and J2 cycles, that is, only

After a value assignment operation, no update operation is performed.

DP [I] [J] = max (DP [I-1] [J], DP [I] [J-1]) however, you can continually assign the previously obtained maximum value to the back of the loop;

For (Int J = 1; j <= LB; j ++)
If (A [I-1] = B [J-1])
{
DP [I] [J] = max (DP [I] [J], DP [I-1] [J-1] + 1 );
}
Else DP [I] [J] = max (DP [I] [J-1], DP [I-1] [J]); // pay attention to this section, this is to match 2-segment arrays and get them if they are smaller than it.
}
The Code is as follows:

 

# Include <cstdio> # include <cstring> using namespace STD; # define n 1001 # define max (A, B) A> B? A: B; int DP [N] [N]; char a [n], B [N]; int main () {While (scanf ("% S % s ", a, B )! = EOF) {memset (DP, 0, sizeof (DP); int LA = strlen (A), lB = strlen (B); For (INT I = 1; I <= La; I ++) {for (Int J = 1; j <= LB; j ++) if (a [I-1] = B [J-1]) {DP [I] [J] = max (DP [I] [J], DP [I-1] [J-1] + 1 );} else DP [I] [J] = max (DP [I] [J-1], DP [I-1] [J]); // pay attention to this section, this is to match the two-segment array and get the result if they are smaller than it} printf ("% d \ n", DP [la] [LB]);} return 0 ;}
View code

 

But if we want to constantly find the matched character, we need a function to recursively find the previous matched character, here we introduce a t [N] [N] flag to help us determine when to perform recursion.

This is a recursive function.

 1 void output(int len1,int len2) 2 { 3     if(len1==0||len2==0) return; 4     if(T[len1][len2]==1){ 5         output(len1-1,len2-1); 6         printf("%c\n",a[len1-1]); 7     } 8     else if(T[len1][len2]==2) output(len1-1,len2); 9     else output(len1,len2-1);10 }

This is a value assigned when T [] [] is used to find the oldest sequence.

This method is used in the second question, so we will not detail it in detail.

2. poj 2250

Http://vjudge.net/problem/viewProblem.action? Id = 17325

This is different from the above: This is for 2 heap strings, find as many identical strings as possible and Output

Output (INT, INT) is used for recursive calling.

The Code is as follows:

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <string> 5 using namespace std; 6 #define N 103 7 string a[N],b[N]; 8 int dp[N][N],T[N][N],len1,len2,cnt=0; 9 10 void match(int len1,int len2)11 {12     memset(dp,0,sizeof(dp));13     for(int i=1;i<=len1;i++)14     {15         for(int j=1;j<=len2;j++)16         {17             if(a[i-1]==b[j-1]) dp[i][j]=dp[i-1][j-1]+1,T[i][j]=1;18             else{19                 if(dp[i-1][j]>dp[i][j-1]) dp[i][j]=dp[i-1][j],T[i][j]=2;20                 else dp[i][j]=dp[i][j-1],T[i][j]=3;21             }22         }23     }24 }25 void output(int m,int n)26 {27     if(m==0||n==0) return;28     if(T[m][n]==1){29         output(m-1,n-1);30         cnt++;31         if(cnt==dp[len1][len2])32             cout<<a[m-1]<<endl;33         else cout<<a[m-1]<<‘ ‘;34     }35     else if(T[m][n]==2) output(m-1,n);36     else output(m,n-1);37 }38 int main()39 {40     string str;41     while(cin>>str)42     {43         len1=1,len2=0;44         a[0]=str;45         while(cin>>str&&str.at(0)!=‘#‘){46             a[len1++]=str;47         }48         while(cin>>str&&str.at(0)!=‘#‘){49             b[len2++]=str;50         }51         match(len1,len2);52         output(len1,len2);53         //cout<<dp[len1][len2]<<endl;54     }55     return 0;56 }
View code

 

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.