Zipper

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5807    Accepted Submission(s): 2086
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 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.

 
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的代码,解释在代码中

深搜:
#include <iostream>
#include <cstring>
using namespace std;
char s1[],s2[],s3[];
int l1,l2,visit[][];
bool dfs(int i,int j,int k)
{
if(k==l1+l2)return;//s3的下标等于s1加s2长度的时候就表示搜完了
if(visit[i][j])return;//这一步很重要,没了他就超时,深搜讲究的就是不重复搜同一个点
visit[i][j]=;
if(i<l1&&s1[i]==s3[k]&&dfs(i+,j,k+))return;//s1能和s3匹配就递归一次,返回值为1就接着返回,因为返回1表示的就是找到了对的路了
if(j<l2&&s2[j]==s3[k]&&dfs(i,j+,k+))return;//s2能和s3匹配也递归一次
return;
}
int main (void)
{
int n,i,j,k=;
cin>>n;
while(n--&&cin>>s1>>s2>>s3)
{
l1=strlen(s1);
l2=strlen(s2);
for(i=;i<;i++)//初始化标记数组
for(j=;j<;j++)
visit[i][j]=;
cout<<"Data set "<<k++<<": ";
if(dfs(,,))cout<<"yes"<<endl;//直接从三个字符串的第一个开始深搜
else cout<<"no"<<endl;
}
return;
}
DP://DP的想法就是DP[i][j]代表s1[i]和s2[j]这两个点同ss[i+j]匹配的当前状态
如匹配catrtee,不看其他杂乱的最终就得到这样个图(实际上还有些数字,因为可能是从中间开始匹配成功的)
    c a t
  0 1 2 0
t 0 0 3 0
r 0 0 4 5
e 0 0 0 6
e 0 0 0 7
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int l1,l2,l3;
char s1[],s2[],ss[];
int main (void)
{
int i,j,n,m=,dp[][];
scanf("%d",&n);
while(n--&&scanf("%s%s%s",s1,s2,ss))
{
memset(dp,,sizeof(dp));
l1=strlen(s1);
l2=strlen(s2);
l3=strlen(ss);
if(ss[]==s1[])dp[][]=;//如果s1第一个和ss配上就标记初始状态“s1成功一个,s2成功0个”为1
        if(ss[]==s2[])dp[][]=;//同上
for(i=;i<=l1;i++)//有了初始状态之后就进行下面的递归
for(j=;j<=l2;j++)
{
if(i>&&s1[i-]==ss[i+j-])//s1没完,就用s1来匹配
dp[i][j]=max(dp[i][j],dp[i-][j]+);//取当前点的值与由前一个状态递推过来的值的最大值,因为有的从中间开始匹配成功的树值会干扰结果
if(j>&&s2[j-]==ss[i+j-])
dp[i][j]=max(dp[i][j],dp[i][j-]+);
}
printf("Data set %d: ",m++);
if(dp[l1][l2]==l3)
cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return;
}
 


最新文章

  1. 攻城狮在路上(壹) Hibernate(七)--- 通过Hibernate操纵对象(下)
  2. Oracle学习笔记(一)
  3. 尝试EJB整合Mybatis部署时报错:获得带有类加载器MybatisUtil的ModuleClassLoader的反射信息出错,请问大神如何解决
  4. 11、WebView 使用总结
  5. Cracking the coding interview--Q2.3
  6. hdu 5532 Almost Sorted Array(模拟)
  7. load_library(linker.cpp:759): library &quot;libmaliinstr.so&quot; not found
  8. HDOJ 1384 差分约束
  9. accp8.0转换教材第11章JAjax加护扩展理解与练习
  10. python接口自动化(六)--发送get请求接口(详解)
  11. SQL开发——SQL语法
  12. Power Query Advanced Editor键盘快捷键
  13. (13)Python文件操作
  14. ELK日志框架(1):安装Elasticsearch组建单服务器多节点集群
  15. 有关Struts下载文件时报错问题
  16. CentOS7使用httpd apache 和firewalld打开关闭防火墙与端口
  17. linux基础02-bash特性
  18. 学习笔记之100 TOP Ikm C++ Online Test Questions
  19. Linux debugger lldb
  20. golang之切片与排序

热门文章

  1. 74HC595的中文资料
  2. [置顶] Android资源文件分析
  3. UVA - 12230 Crossing Rivers (期望)
  4. &lt;input type=&quot;text&quot;&gt;文本输人框
  5. C#_会员管理系统:开发七(用户分类)
  6. BZOJ 1567: [JSOI2008]Blue Mary的战役地图( 二分答案 + hash )
  7. BZOJ 1669: [Usaco2006 Oct]Hungry Cows饥饿的奶牛( LIS )
  8. [C#参考]委托机制
  9. python安装zlib一直无效
  10. js 几个特殊情况