hdu-5707-Combine String

                                                          Combine String

                                                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
                                                                     Total Submission(s): 151    Accepted Submission(s): 78


Problem Description
Given three strings a, b and c, your mission is to check whether c is the combine string of a and b.
A string c is said to be the combine string of a and b if and only if c can be broken into two subsequences, when you read them as a string, one equals to a, and the other equals to b.
For example, ``adebcf'' is a combine string of ``abc'' and ``def''.
 

Input
Input file contains several test cases (no more than 20). Process to the end of file.
Each test case contains three strings a, b and c (the length of each string is between 1 and 2000).
 

Output
For each test case, print ``Yes'', if c is a combine string of a and b, otherwise print ``No''.
 

Sample Input
 
 
abc def adebcf abc def abecdf
 

Sample Output
 
 
Yes No
题意:给定a,b,c三个串,问c能否按序分成a和b串,不要求连续。
题目链接:Combine String
解题思路:
直接模拟,1.统计处a,b,c串各个字符出现的次数,看a,b串的某个字符出现次数的和是否与c串中的相等。
2.从头往后扫描c串,看是否能按序找到a,b串。
代码:
//hdu-5707-Combine String

#include<map>
#include<iostream>
#include<string>
#include<cstdio>

using namespace std;

map<char,int> mpa,mpb,mpc;
int main()
{
    string a,b,c;
    int i,lena,lenb,lenc,x,y,flag;
    while(cin>>a>>b>>c){
        flag=1,x=y=0;
        mpa.clear(),mpb.clear(),mpc.clear();
        lena=a.length(),lenb=b.length(),lenc=c.length();
        for(i=0; i<lena ; i++)
            mpa[a[i]]++;
        for(i=0; i<lenb ; i++)
            mpb[b[i]]++;
        for(i=0; i<lenc ; i++){
            mpc[c[i]]++;
            if(x < lena && c[i] == a[x] ) x++;
            if(y < lenb && c[i] == b[y]) y++;
        }
        map<char,int> :: iterator it=mpc.begin();
        for( ; it!=mpc.end() ;it++){
            if(mpa[it->first] + mpb[it->first] != it->second ){
                flag=0;
                break;
            }
        }
       //printf("x=%d y=%d\n",x,y);
       // printf("lena = %d lenb= %d lenc = %d\n",lena,lenb,lenc);
        if(flag && x==lena && y == lenb) cout<<"Yes";
        else                                cout<<"No";
        cout<<endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值