交替字符串

题目详情

如果字符串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


#include<stdio.h>
int flag;

void f(char* str1,char* str2,char *str3)
{
    if(*str3)
    {
        if(*str1==*str3)f(str1+1,str2,str3+1);
        if(*str2==*str3)f(str1,str2+1,str3+1);

    }
    if(*str1==0 && *str2==0 && *str3==0)flag=1;
    return;
}

void main ()
{
    char *str1=calloc(100,sizeof(char));
    char *str2=calloc(100,sizeof(char));
    char *str3=calloc(200,sizeof(char));

    while(scanf("%s",str1)!=EOF)
    {
        scanf("%s",str2);
        scanf("%s",str3);
        flag=0;
        f(str1,str2,str3);
        if(flag==1)printf("YES\n");
        else printf("NO\n");
    }
    
    return 0;
}


类似题目:CodeForces 320A - Magic Numbers

A. Magic Numbers

time limit per test: 2 seconds

memory limit per test: 256 megabytes

A magic number is a number formed by concatenation of numbers 1, 14 and 144. We can use each of these numbers any number of times. Therefore 14144, 141414 and 1411 are magic numbers but 1444, 514 and 414 are not.You're given a number. Determine if it is a magic number or not.InputThe first line of input contains an integer n, (1 ≤ n ≤ 10^9). This number doesn't contain leading zeros.OutputPrint "YES" if n is a magic number or print "NO" if it's not.

Sample test(s)
Input
114114
Output
YES
Input
1111
Output
YES
Input
441231
Output
NO

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <list>
#include <queue>
#include <utility>
#include <sstream>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

string str;
int border;
bool isM(int n)
{	
	if(n+2==border && str[n]=='1' && str[n+1]=='4' && str[n+2]=='4')return true;
	if(n+1==border && str[n]=='1' && str[n+1]=='4')return true;
	if(n==border && str[n]=='1' )return true;

	bool ans=false;
	if(n<border && str[n]=='1')ans=isM(n+1);
	if(n+1<border && str[n]=='1' && str[n+1]=='4')ans=isM(n+2);
	if(n+2<border && str[n]=='1' && str[n+1]=='4' && str[n+2]=='4')ans=isM(n+3);

	return ans;
}

int main()
{	 
	cin>>str;
	border=str.size()-1;
	string s=isM(0)?"YES":"NO";
	cout<<s<<endl;
  	return 0;
}



上题解复杂了:

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <vector>
#include <iomanip>
#define PI acos(-1.0)
using namespace std;
int n;
int main(){
	cin>>n;
	while(n){
		if (n%10==1) n/=10;
		else if (n%100==14) n/=100;
		else if (n%1000==144) n/=1000;
		else{
			cout<<"NO\n";
			return 0;
		}
	}
	cout<<"YES\n";
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值