【字符串DP】——hdu1501——多字符串匹配——好题
Zipper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9345 Accepted Submission(s): 3313
Problem Description
Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.
For example, consider forming "tcraete" from "cat" and "tree":
String A: cat
String B: tree
String C: tcraete
As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming "catrtee" from "cat" and "tree":
String A: cat
String B: tree
String C: catrtee
Finally, notice that it is impossible to form "cttaree" from "cat" and "tree".
For example, consider forming "tcraete" from "cat" and "tree":
String A: cat
String B: tree
String C: tcraete
As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming "catrtee" from "cat" and "tree":
String A: cat
String B: tree
String C: catrtee
Finally, notice that it is impossible to form "cttaree" from "cat" and "tree".
Input
The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line.
For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.
For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.
Output
For each data set, print:
Data set n: yes
if the third string can be formed from the first two, or
Data set n: no
if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.
Data set n: yes
if the third string can be formed from the first two, or
Data set n: no
if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.
Sample Input
3
cat tree tcraete
cat tree catrtee
cat tree cttaree
Sample Output
Data set 1: yes
Data set 2: yes
Data set 3: no
(转)
题意:给你三个单词,在前两个单词相对顺序不变的情况下,是否能组成第三个单词?第三个单词的长度是前两个单词的长度和
思路:普通搜索和记忆化搜索都是可以的,主要是要剪枝,判断第三个单词的最后一个字母是否由前两个单词的最后一个字母组成......
代码如下:
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; char s[4][500]; int dp[500];//记录目标状态的在 i 位置是否成功匹配 int len1,len2,len0,flag=0; //通过栈实现记忆化搜索 int dfs(int num0,int num1,int num2)//状态:每个字符串 { //该长度可以匹配到 if(dp[num2]>0) return 1; //两个串匹配完了且此时串3未匹配完 if(num1>=len1&&num0>=len0) return 0; //1串用完,2串无法匹配 if(num0==len0&&s[1][num1]!=s[2][num2]) return 0; //2串用完,1串无法匹配 if(num1==len1&&s[0][num0]!=s[2][num2]) return 0; //两个穿的下一位皆不匹配 if(s[0][num0]!=s[2][num2] && s[1][num1]!=s[2][num2]) return 0; if(num0<len0 && s[0][num0]==s[2][num2])//串1可匹配 { dp[num2]=dfs(num0+1,num1,num2+1); } if(num1<len1 && s[1][num1]==s[2][num2])//串2可匹配 { dp[num2]=dfs(num0,num1+1,num2+1); } return dp[num2]; } int main() { int text,f=0; scanf("%d",&text); while(text--) { scanf("%s%s%s",s[0],s[1],s[2]); len0=strlen(s[0]); len1=strlen(s[1]); len2=strlen(s[2]); flag=0; memset(dp,0,sizeof(dp)); dp[len2]=1; if(len0+len1==len2&& (s[0][len0-1]==s[2][len2-1] || s[1][len1-1]==s[2][len2-1]) )//剪枝 flag=dfs(0,0,0); printf("Data set %d: ",++f); if(flag==1) printf("yes\n"); else printf("no\n"); } return 0; }
题后感:
动态规划在字符串匹配上的应用。
而且需要顺序一致,重新排列。
因为长度1+2=3,所以只要有一个不匹配,就可以直接退出循环了。
记忆化搜索的思路还是从求最长公共子序列演变而来的
记录三个字符串分别匹配到哪里了
然后就是紫怡代码里的各种剪枝了。
这道DP真心很难看出来,
不过如果淡化记忆化搜索和普通搜索的界限,那么就是容易把思路转变过来

浙公网安备 33010602011771号