64,649
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
char str1 [101] ;int len1 = 0 ;
char str2 [101] ;int len2 = 0 ;
char str3 [201] ;int len3 = 0 ;
bool getRes(int i3,int i1,int i2)
{
if(i3 == len3 && i1 == len1 && i2 == len2)return true ;
if(i3 > len3 || i1 > len1 || i2 > len2)return false ;
bool res = false ;
if(str3[i3] == str1[i1])res = getRes(i3+1,i1+1,i2);
if((!res)&&str3[i3] == str2[i2])res = getRes(i3+1,i1,i2+1);
return res ;
}
int main()
{
while(cin>>str1>>str2>>str3)
{
len1 = strlen(str1) ;
len2 = strlen(str2) ;
len3 = strlen(str3) ;
if(getRes(0,0,0))cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
bool fun(const string &s1, const string &s2, const string &s3, int i1=0, int i2=0, int i3=0)
{
if (s1.size() + s2.size() != s3.size())
return false;
if (s1.size() == i1)
return s2.substr(i2) == s3.substr(i3);
if (s2.size() == i2)
return s1.substr(i1) == s3.substr(i3);
if (s3[i3] == s1[i1] && s3[i3] == s2[i2])
return fun(s1, s2, s3, i1+1, i2, i3+1) || fun(s1, s2, s3, i1, i2+1, i3+1);
else if (s3[i3] == s1[i1])
return fun(s1, s2, s3, i1+1, i2, i3+1);
else if (s3[i3] == s2[i2])
return fun(s1, s2, s3, i1, i2+1, i3+1);
else
return false;
}
int main()
{
cout<<fun("a", "b", "ab")<<true<<endl;
cout<<fun("a", "b", "ba")<<true<<endl;
cout<<fun("a", "b", "bac")<<false<<endl;
cout<<fun("abc", "bcd", "abcdbc")<<true<<endl;
cout<<fun("abc", "bcd", "abcbcd")<<true<<endl;
cout<<fun("abcbc", "bcd", "abcbcdbc")<<true<<endl;
return 0;
}
#include <iostream>
using namespace std;
char str1 [101] ;int len1 = 0 ;
char str2 [101] ;int len2 = 0 ;
char str3 [201] ;int len3 = 0 ;
bool res = false ;
void getRes(int i3,int i1,int i2)
{
if(i3 == len3 && i1 == len1 && i2 == len2){res = true ;return ;} //条件(1)
else if(i3 == len3 && (i1 != len1 || i2 != len2))return;
if(i3 > len3 || i1 > len1 || i2 > len2)return ;
if(str3[i3] == str1[i1])getRes(i3+1,i1+1,i2);
if(str3[i3] == str2[i2])getRes(i3+1,i1,i2+1);
return ;
}
int main()
{
while(cin>>str1>>str2>>str3)
{
len1 = strlen(str1) ;
len2 = strlen(str2) ;
len3 = strlen(str3) ;
res = false;
getRes(0,0,0);
if(res)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
题目详情
如果字符串str3能够由str1和str2中的字符按顺序交替形成,那么称str3为str1和str2的交替字符串。例如str1="abc",str2="def",那么"adbecf", "abcdef", "abdecf", "abcdef", "adefbc"等等都为str1和str2的交替字符串。更形式化的,str3的生成算法如下:
str3=""
while str1不为空 or str2不为空:
把str1或str2的首字符加入到str3,并从str1或str2中删除相应的字符
end
给定str1, str2,和str3,判断str3是否为str1和str2的交替字符串。
输入格式:
多组数据,每组数据三行,分别是str1,str2,str3。str1,str2的长度在[1..100]范围内,str3的范围在[1..200]范围内。字符串只包含小写英文字母。
输出格式:
每组数据输出一行YES或者NO。
答题说明
输入样例
a
b
ab
a
b
ca
输出样例:
YES
NO
链接:http://hero.csdn.net/?ref=toolbar