Zipper
2015-08-06 21:05
288 查看
[align=center][/align] |
ZipperTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8072 Accepted Submission(s): 2843 [align=left]Problem Description[/align] 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". [align=left]Input[/align] 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. [align=left]Output[/align] 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. [align=left]Sample Input[/align] 3 cat tree tcraete cat tree catrtee cat tree cttaree [align=left]Sample Output[/align] Data set 1: yes Data set 2: yes Data set 3: no |
代码:
#include<stdio.h> #include<string.h> char A[201],B[201],C[402],t1,t2,t3,flot; void dfs(int len1,int len2,int len3){ if(flot)return; if(len3>=t3){ flot=1; return; } if(C[len3]==A[len1]){ dfs(len1+1,len2,len3+1); } if(C[len3]==B[len2]){ dfs(len1,len2+1,len3+1); } } int main(){ int n,tot=0; scanf("%d",&n); while(n--){flot=0;tot++; memset(A,0,sizeof(A)); memset(B,0,sizeof(B)); memset(C,0,sizeof(C)); scanf("%s%s%s",A,B,C); t1=strlen(A); t2=strlen(B); t3=strlen(C); if(t1+t2==t3&&(A[t1-1]==C[t3-1]||B[t2-1]==C[t3-1]))dfs(0,0,0); if(flot)printf("Data set %d: yes\n",tot); else printf("Data set %d: no\n",tot); } return 0; }
大神的就能ac,感觉都差不多啊:
#include<stdio.h> #include<string.h> char a[220],b[220],c[220*2]; bool flag; int len1,len2,len3; void dfs(int x,int y,int len) { if(flag) return; if(len>=len3) { flag=true; 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,cot=0; scanf("%d",&t); while(t--) { scanf("%s%s%s",a,b,c); len1=strlen(a); len2=strlen(b); len3=strlen(c); if(len1+len2!=len3) { printf("Data set %d: no\n",++cot); continue; } flag=false; if(a[len1-1]==c[len3-1]||b[len2-1]==c[len3-1]) { dfs(0,0,0); } if(flag) printf("Data set %d: yes\n",++cot); else printf("Data set %d: no\n",++cot); } }
相关文章推荐
- string to float
- 本地动态SQL(Open for等)如何获取SQL的定义属性,包括栏位名称和数据类型
- 2015年成都-雅安-新都桥自驾游 蓝天、白云、绿草、牦牛
- [leedcode 215] Kth Largest Element in an Array
- CentOS、Mysql性能分析
- 关于英雄联盟“服务器繁忙 暂时无法登录 请稍候再试”的解决方法
- UVA 11134
- matlab基础学习笔记
- Java中变量的作用域
- 图像处理和计算机视觉中的经典论文
- [转]MS SQL数据库备份和恢复存储过程
- JAX-WS编写webservice
- WebView之2
- 超级楼梯
- 视频转换和视频直播
- php5中的clone 浅拷贝 深拷贝
- java中关于方法重载和renturn用法
- poj2251 dungeon master【BFS】~
- 《机器学习》(Machine Learning)——Andrew Ng 斯坦福大学公开课学习笔记(二)
- 设计模式入门--观察者模式