描述
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".
输入
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, 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.
样例输入
3 cat tree tcraete cat tree catrtee cat tree cttaree
样例输出
Data set 1: yes Data set 2: yes Data set 3: no
//纯手工制作 ,提交就错了10次,努力吧
//我机器跑一次205的那答案,就用10多分钟,昨天弄到了晚12点呢
//以字符串3来比较2和1串,字符不同的好弄,相同时,到底消哪个
//动态规划不会想,就用自己的方法 这程序还有须改进之处
//1,2串的字符在3中都能按顺序找到就是yes了
//Writed by Wangzhimin Date:2024/01/01
//Happy New Year!^-^
#include<bits/stdc++.h>
using namespace std;
int n;
char s1[205],s2[205],s3[410];//存字符串
int main()
{
int cnt=0;//计数
scanf("%d",&n);
while(n--)
{
cnt++;
//初始化字串
memset(s1,'\0',sizeof(s1));
memset(s2,'\0',sizeof(s2));
memset(s3,'\0',sizeof(s3));
char *p1,*p,*p2,*p3;//指针
scanf("%s%s%s",s1,s2,s3);
p1=s1;p2=s2;p3=s3,p=s3;
int k3=strlen(s3);
int k1=strlen(s1);//这下面两个没用到,应比较3与1和2的和是不是相等
int k2=strlen(s2);//程序设计不是很完整
printf("Data set %d: ",cnt);//先输出前面的
int t=0,i;//计数的
for( i=0;i<k3;i++)//串的长度
{
// cout<<endl<<*p1<<" "<<*(p+i)<<" "<<*p2<<" "<<"h "<<i<<endl;
if((*p1!=*(p+i))&&(*p2!=*(p+i)))//如果没有相同的了
break;
if((*p1==*(p+i))&&(*p2!=*(p+i)))//1相同2不同
{
p1++;
continue;
}
if(*p2==*(p+i)&&*p1!=*(p+i))//2相同1不同
{
p2++;
continue;}
if((*p2==*(p+i))&&(*p1==*(p+i)))//1,2都与3中的字符相同
{
t=0;
while((*(p2+t)==*(p+i+t))&&(*(p1+t)==*(p+i+t)))
t++;//直到不同
if(*(p1+t)==*(p+i+t))//如果1还有相同的
{
p1++;
continue;
}
if(*(p2+t)==*(p+i+t))//如果2还有相同的
{
p2++;
continue;
}
else //都不与3串中的字符相同,这是预判
{
int flag=0;//设个标志,看哪个删除
for(int m=1;m<=t;m++)//3串中后面有和前面相同的吗
{//有的话,先长这个指针
if(*(p2+t)==*(p+t+i+m))
flag=1;
// cout<<endl<<*(p2+t)<<"111"<<*(p+t+i+m)<<endl;
}
if(flag==1)
{
p2++;
continue;
}
else{//没有,长这个指针
p1++;
continue;
}
}
}
}
// cout<<endl<<*p1<<" "<<*(p+i)<<" "<<*p2<<" "<<"here"<<i<<endl;
if(i>=k3)//到了3串的尾了吗
printf("yes\n");
else
printf("no\n");
}
}
放在动规中,但不是动规啊,以后改进。