hduoj-1501【dfs】【dp】
来源:互联网 发布:dede源码素材资源网 编辑:程序博客网 时间:2024/05/22 00:22
题目链接:点击打开链接
Zipper
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9960 Accepted Submission(s): 3571
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
3cat tree tcraetecat tree catrteecat tree cttaree
Sample Output
Data set 1: yesData set 2: yesData set 3: no
大意:给你三个字符串,判断第三个字符串是否是前面两个字符串组成的,且前面两个字符串的字符在第三个字符串中的顺序不变。
/*#include<cstdio>#include<algorithm>#include<cstring>using namespace std;bool flag;char a[222],b[222],c[222*2];int len1,len2,len3;void dfs(int x,int y,int len){if(flag)return ;if(len>=len3){flag=1;return ;}if(a[x]==c[len])dfs(x+1,y,len+1);if(b[y]==c[len])dfs(x,y+1,len+1);}int main(){int t,text=0;scanf("%d",&t);while(t--){scanf("%s%s%s",a,b,c);len1=strlen(a);len2=strlen(b);len3=strlen(c);printf("Data set %d: ",++text);if(len1+len2!=len3){puts("no");continue;}flag=0;if(a[len1-1]==c[len3-1]||b[len2-1]==c[len3-1])dfs(0,0,0);if(flag)puts("yes");elseputs("no");}return 0;}*/#include<cstdio>#include<algorithm>#include<cstring>using namespace std;char a[222],b[222],c[222*2];bool dp[222][222];int main(){int t,text=0;scanf("%d",&t);while(t--){scanf("%s%s%s",a+1,b+1,c+1);int len1=strlen(a+1);int len2=strlen(b+1);int len3=strlen(c+1);//printf("%d--%d--%d\n",len1,len2,len3);//printf("%c--%c--%c--\n",a[len1],b[len2],c[len3]);memset(dp,0,sizeof(dp));for(int i=1;i<=len1;i++){if(a[i]==c[i])dp[i][0]=1;}for(int i=1;i<=len2;i++){if(b[i]==c[i])dp[0][i]=1;}for(int i=1;i<=len1;i++){for(int j=1;j<=len2;j++){if(dp[i-1][j]&&(a[i]==c[i+j]))dp[i][j]=1;if(dp[i][j-1]&&(b[j]==c[i+j]))dp[i][j]=1;}}printf("Data set %d: ",++text);if(dp[len1][len2])puts("yes");elseputs("no");} return 0;}
0 0
- hduoj-1501【dfs】【dp】
- hduoj 1241 经典dfs
- hduoj-5311【dfs】
- HDUOJ 2089 数位DP
- hduoj-1466【DP】
- 【HDUOJ 6035】 Colorful Tree 【DFS】
- HDU 1501 Zipper(DP,DFS)
- HDU 1501 Zipper(DP,DFS)
- HDUoj 5113 Black And White (dfs&剪枝
- 【HDUOJ]1010-Tempter of bone DFS+剪枝
- DFS+DP
- hduoj-2084 数字塔问题 DP
- HDUOJ 1005 Number Sequence(DP求公式)
- HDUOJ
- HDUoj
- hdu 1501 Zipper(dfs或dp)
- HDUoj 5723 Abandoned country(最小生成树+dfs
- HDUoj 2089 不要62(打表)(数位DP)
- BZOJ 4033 树上染色 (题解)(树上动规)
- 从github上的优秀实例看MVP模式
- MySQL5.7版本的下载安装与配置
- 模运算与幂运算
- Ubuntu中root用户和user用户的相互切换
- hduoj-1501【dfs】【dp】
- 六天带你玩转Mysql笔记--第二天
- 欢迎使用CSDN-markdown编辑器
- 同余方程
- 在ARM开发板上实现LCD+USB键盘
- Android Hybrid 方案之 离线文件加载
- Spark Streaming 的玫瑰与刺
- sort排序用法
- Android Studio 2.3 签名打包问题