个人信息:就读于燕大本科软件工程专业 目前大三;
本人博客:百度搜索“cqs_2012”即可;
个人爱好:酷爱数据结构和算法,希望将来搞科研为人民作出自己的贡献;
博客内容:英雄会之交替字符串;
博客时间:2014-3-26
做题目不是为了得奖,为了历练自己。
题目详情如果字符串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。
个人认为这道题很简单,但是需要考虑各种细节;
str1,str2的长度在[1..100]范围内 str3的范围在[1..200]范围内 符串只包含小写英文字母个人思路如下(突然发现博客上传照片不能显示了,痛。。。)
// function: test for problem bool Str3_for_Str1andStr2(string str1,string str2,string str3) { if( str1.length()>0 && str2.length()>0 && str3.length()>0 && str3.length() == str1.length() + str2.length() ) { size_t m1=0,m2=0,m3=0; while(m3 < str3.length()) { if( str3[m3] == str1[m1] ) { m3++; m1++; } else if( str3[m3] == str2[m2] ) { m3++; m2++; } else return false ; } return true ; } else { cout<<"exception of function Str3_for_Str1andStr2 input"<<endl; return false; } }
今天不知道咋了,突然发现程序截图不能上传了,痛呀。。。
结果文本如下
asfsdfsdf
sfs
asdfsdfasfas
0
aab
bba
bbaaab
1
aaa
bbb
baabba
1
test.cpp
// include: head file #include<iostream> #include<string> using namespace std; // function: test for problem bool Str3_for_Str1andStr2(string str1,string str2,string str3); // function: main int main() { string str1,str2,str3; while(cin>>str1>>str2>>str3) cout<<Str3_for_Str1andStr2(str1,str2,str3)<<endl; system("pause"); return 0; } // function: test for problem bool Str3_for_Str1andStr2(string str1,string str2,string str3) { if( str1.length()>0 && str2.length()>0 && str3.length()>0 && str3.length() == str1.length() + str2.length() ) { size_t m1=0,m2=0,m3=0; while(m3 < str3.length()) { if( str3[m3] == str1[m1] ) { m3++; m1++; } else if( str3[m3] == str2[m2] ) { m3++; m2++; } else return false ; } return true ; } else { cout<<"exception of function Str3_for_Str1andStr2 input"<<endl; return false; } }
原文:http://blog.csdn.net/cqs_experiment/article/details/22147885