Codeforces Round #438 A. Bark to Unlock

题意:给你一个原串和n个子串,问你这n个子串任意组合起来能不能使原串出现,串的长度为2。

 

Examples

 

Input
ya
4
ah
oy
to
ha

 

Output
YES

 

Input
hp
2
ht
tp

 

Output
NO

 

Input
ah
1
ha

 

Output

 

YES

思路: 首先如果输入的就有原串那么肯定可以;若没有,那么就记录一下每个子串的第一个字母是否等于原串的第二个字母,
     第二个字母是否等于原串的
第一个字母,因为这样可以拼接起来构成原串,按照最后一个样例,好像每个子串可以用多次。

代码:
#include<iostream>
#include<string.h>
using namespace std;

int main(){
    char c1,c2,a,b;
    int n,ans1=0,ans2=0;
    cin>>c1>>c2>>n;
    for(int i=0;i<n;i++){
        cin>>a>>b;
        if(a==c1&&b==c2){
            cout<<"YES"<<endl;
            return 0;
        }
        if(b==c1)ans2++;
        if(a==c2)ans1++;
    }
    if(ans1&&ans2)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}


 

posted @ 2017-10-07 11:43  ljy3268  阅读(122)  评论(0编辑  收藏  举报