HDU 6170 ---- Two strings (DP), hdu6170 ---- two

Source: Internet
Author: User

HDU 6170 ---- Two strings (DP), hdu6170 ---- two

Question Link

 

Problem DescriptionGiving two strings and you shoshould judge if they are matched.
The first string contains lowercase letters and uppercase letters.
The second string contains lowercase letters, uppercase letters, and special symbols: "." and "*".
. Can match any letter, and * means the front character can appear any times. for example, ". B "can match" acb "or" abb "," a * "can match" a "," aa "and even empty string. ("*" will not appear in the front of the string, and there will not be two consecutive "*".

 

InputThe first line contains an integer T implying the number of test cases. (T ≤ 15)
For each test case, there are two lines implying the two strings (The length of the two strings is less than 2500 ).

 

OutputFor each test case, print "yes" if the two strings are matched, otherwise print "no ".

 

Sample Input3aaa * abba. * abbaab

 

Sample OutputYesyesno: two strings are given to determine whether a match exists. The first string contains only lower-case and upper-case characters, and the second string contains lower-case and upper-case characters '. 'and '*','. 'can match any character.' * 'indicates that the character before' * 'can be repeated multiple times. For example, a * can match a, aa, aa ...... And empty strings (Note: The second string does not start with '*' or two consecutive '*'). Idea: Consider DP, each time according to 1 ~ Where can I enable string B to reach ~ Where can I + 1 string B reach. The Code is as follows:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int N=2505;char a[N],b[N];int len1,len2;int dp[N][N];int main(){    int T; cin>>T;    while(T--){       scanf("%s%s",a+1,b+1);       len1=strlen(a+1);       len2=strlen(b+1);       memset(dp,0,sizeof(dp));       dp[0][0]=1;       for(int i=1;i<=len2;i++)       {           if(b[i]=='.')           {              for(int j=0;j<=len1;j++)              {                  if(dp[i-1][j]) dp[i][j+1]=1;              }           }           else if(b[i]=='*')           {              for(int j=0;j<=len1;j++)              {                  if(dp[i-1][j])                  {                     dp[i][j]=1;                     dp[i][j-1]=1;                     while(a[j+1]==a[j]) dp[i][j+1]=1,j++;                  }              }           }           else           {              for(int j=0;j<=len1;j++)              {                  if(!dp[i-1][j]) continue;                  if(a[j+1]==b[i]) dp[i][j+1]=1;                  else if(b[i+1]=='*') dp[i+1][j]=1;              }           }       }       if(dp[len2][len1]) puts("yes");       else puts("no");    }    return 0;}/*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.**/

The simulated deep search I used in the competition will time out, but if the answer is "yes", it will be quickly calculated and will not time out; if it is "no, it searches for all cases and times out. In this case, we can use a variable to record the number of recursion times. If the number is greater than a certain number, the default value is "no" and the search is exited. (Of course, this approach is not a positive solution. The brain hole is wide open. If there is a lot of data, it will definitely not be enough ~)

The Code is as follows:

# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> using namespace std; const int N = 2505; char a [N], B [N]; int len1, len2; int h [N]; int c; int dfs (int I, int j) {c +++; if (c> 1000000) return 0; // The default value is "no". if (I <len1 & j> = len2) return 0; if (I> = len1) {if (j> = len2) return 1; if (j = len2-1 & B [j] = '*') return 1; if (j = len2-1 & B [j]! = '*') Return 0; if (j <len2-1) {if (B [j] = '*' & h [j + 1]) return 1; else if (B [j]! = '*' & H [j]) return 1; else return 0 ;}} if (B [j] = '. ') {B [j] = a [I]; int f = dfs (I + 1, j + 1); B [j] = '. '; return f;} if (B [j] =' * ') {if (a [I] = B [J-1]) {if (dfs (I + 1, j) return 1; if (dfs (I, j + 1) return 1; if (dfs (I-1, j + 1 )) return 1 ;}else {if (dfs (I-1, j + 1) return 1; if (dfs (I, j + 1) return 1 ;}} if (a [I] = B [j]) return dfs (I + 1, j + 1); else if (B [j + 1] = '*') return dfs (I, j + 2); else return 0;} int main () {int T; cin> T; while (T --) {scanf ("% s", a, B); c = 0; len1 = strlen (); len2 = strlen (B); int flag = 1; for (int I = len2-1; I> = 0; I --) {if (! Flag) h [I] = 0; else if (B [I] = '*') {h [I] = 1; h [I-1] = 1; I --;} else {h [I] = 0; flag = 0 ;}} int ans = dfs (0, 0); if (ans) puts ("yes "); else puts ("no");} 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.