HDU 1501 Zipper
2015-08-15 21:29
316 查看
Zipper
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Submission(s) : 23 Accepted Submission(s) : 6
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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".
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 perline.
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.
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
这题完全是看别人的DP思想写出来的,对于刚接触DP的我,还是不太会写,代码是搞懂了自己敲的。
大体思想是,sum的最后一个元素总是和str1的最后一个元素,或者str2的最后一个元素相等,这就可以看成dp【i-1】【j】或者dp【i】【j-1】的问题
这就是把问题转换到了小问题上,有了子结构。
上AC代码
#include <stdio.h>
#include <string.h>
int dp[205][205];
int main()
{
char str1[205],str2[205],sum[405];
int n,i,j,k,len1,len2;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%s %s %s",str1,str2,sum);
len1=strlen(str1);
len2=strlen(str2);
memset(dp,0,sizeof(dp));
for(j=1;j<=len1;j++)
dp[j][0]=(str1[j-1]==sum[j-1]);
for(k=1;k<=len2;k++)
dp[0][k]=(str2[k-1]==sum[k-1]);
for(j=1;j<=len1;j++)
{
for(k=1;k<=len2;k++)
{
dp[j][k]=(str1[j-1]==sum[j+k-1] && dp[j-1][k] || str2[k-1]==sum[j+k-1] && dp[j][k-1]);
}
}
if(dp[len1][len2])
printf("Data set %d: yes\n",i);
else
printf("Data set %d: no\n",i);
}
return 0;
}
相关文章推荐
- Mac 安装 opencv
- Linux文件查找
- vijos - P1164曹冲养猪(中国剩余定理 + python)
- HDU 2227 Find the nondecreasing subsequences
- csdn论坛积分等级制度
- Android 中的Drawable资源
- POJ-1410
- 携手互联网企业10巨头设VC基金
- 10.3QEMU基于VNC的桌面虚拟化原理
- 刀哥多线程全局队列gcd-09-global_queue
- 数组与字符串,判断字符串各个字符是否不同
- Uva 401 Palindromes
- HDU 5391 Zball in Tina Town(判素数)
- 实现reverse字符串
- 数据结构学习笔记――线性表
- 背景滚动视差的一个页面[仿雅虎邮箱主页]
- 10.2KVM嵌套虚拟化原理
- hdoj 2187 悼念512汶川大地震遇难同胞——老人是真饿了
- 刀哥多线程Barrier异步gcd-08-barrier_async
- elnode入门