hdu5707
2016-05-28 23:15
218 查看
Combine String
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 27 Accepted Submission(s): 17
[/b]
[align=left]Problem Description[/align]
Given three strings a,
b
and c,
your mission is to check whether c
is the combine string of a
and b.
A string c
is said to be the combine string of a
and b
if and only if c
can be broken into two subsequences, when you read them as a string, one equals to
a,
and the other equals to b.
For example, ``adebcf'' is a combine string of ``abc'' and ``def''.
[align=left]Input[/align]
Input file contains several test cases (no more than 20). Process to the end of file.
Each test case contains three strings a,
b
and c
(the length of each string is between 1 and 2000).
[align=left]Output[/align]
For each test case, print ``Yes'', if
c
is a combine string of a
and b,
otherwise print ``No''.
[align=left]Sample Input[/align]
abc
def
adebcf
abc
def
abecdf
[align=left]Sample Output[/align]
Yes
No
本题的DP类似于LCS(longest common subsequence)(如果不了解的可以搜一下)。
只是,
f[i][j]表示第一个字符串用了前i个位置(第i个位置已匹配),第二个字符串的前j个位置(第j个位置已匹配)
是否可以对c串成功匹配(成功匹配则必然会匹配到c串的前i+j个位置)。
f[i][j]==1则表示可以成功匹配
f[i][j]==0则表示无法成功匹配
显然,初始只有f[0][0]==1
所以,我们有——
f[i][j]= f[i-1][j]&&(a[i]==c[i+j])
|| f[i][j-1]&&(b[j]==c[i+j])
这样,最终f
[m]==1则为Yes否则为No
【时间复杂度&&优化】
O(nm)
#include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; const int N = 2005; char a ,b ,c ; int dp ; int main() { int i,j,n,m,k; while(~scanf("%s",a+1)) { memset(dp,0,sizeof(dp)); scanf("%s",b+1); scanf("%s",c+1); n=strlen(a+1); m=strlen(b+1); k=strlen(c+1); if(n+m!=k) { puts("No"); continue; } dp[0][0]=1; for(i=0;i<=n;i++) for(j=0;j<=m;j++) { if(i>0&&c[i+j]==a[i]) dp[i][j]|=dp[i-1][j]; if(j>0&&c[i+j]==b[j]) dp[i][j]|=dp[i][j-1]; } if(dp [m]) puts("Yes"); else puts("No"); } return 0; }
相关文章推荐
- 常见bat常识
- 中间件(Middleware)
- LeetCode 22. Generate Parentheses
- ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify
- 比较 N个元素 一个一个地插入到一个堆中 以线性时间建立一个堆 已排序、反序、随机输入的运行时间
- java File类
- JQuery按钮的隐藏与显示
- 数据库完整性
- DOM的基础,属性以及常用方法
- IOS懒加载
- java多线程---停止、暂停一个线程
- 如何获取SQL Server所有的数据库名、表名、字段名及字段类型?
- java操作XML文件
- 关系模型数据库
- Android 关于ListView的一些小总结
- 第6次c++作业
- mysqlbackup
- SQL Server中,如何将所有符合条件的结果拼接成一列并用逗号隔开?
- [麦先生]初学Laravel框架与ThinkPHP框架的不同(2)
- RecycleView使用问题梳理