Codeforces Round #438 (Div. 1 + Div. 2 combined) A. Bark to Unlock(模拟)

97 篇文章 0 订阅

A. Bark to Unlock
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

As technologies develop, manufacturers are making the process of unlocking a phone as user-friendly as possible. To unlock its new phone, Arkady's pet dog Mu-mu has to bark the password once. The phone represents a password as a string of two lowercase English letters.

Mu-mu's enemy Kashtanka wants to unlock Mu-mu's phone to steal some sensible information, but it can only bark n distinct words, each of which can be represented as a string of two lowercase English letters. Kashtanka wants to bark several words (not necessarily distinct) one after another to pronounce a string containing the password as a substring. Tell if it's possible to unlock the phone in this way, or not.

Input

The first line contains two lowercase English letters — the password on the phone.

The second line contains single integer n (1 ≤ n ≤ 100) — the number of words Kashtanka knows.

The next n lines contain two lowercase English letters each, representing the words Kashtanka knows. The words are guaranteed to be distinct.

Output

Print "YES" if Kashtanka can bark several words in a line forming a string containing the password, and "NO" otherwise.

You can print each letter in arbitrary case (upper or lower).

Examples
input
ya
4
ah
oy
to
ha
output
YES
input
hp
2
ht
tp
output
NO
input
ah
1
ha
output
YES
Note

In the first example the password is "ya", and Kashtanka can bark "oy" and then "ah", and then "ha" to form the string "oyahha" which contains the password. So, the answer is "YES".

In the second example Kashtanka can't produce a string containing password as a substring. Note that it can bark "ht" and then "tp" producing "http", but it doesn't contain the password "hp" as a substring.

In the third example the string "hahahaha" contains "ah" as a substring.


题解:

就是说给你一个2位小写字母的串,问你从其他n个2位串是否可以拼接处该串(可以无限取)

思路:

很简单,如果和原串一样或者相反直接yes,否则就看一个的第二个和另一个的第一个是否拼接可以成该串

暂时通过的代码:

#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
#define ll long long
#define INF 1008611111
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
using namespace std;
int main()
{
    int i,j,n,tag=0;
    char s[10],a[105][10];
    int vis[10];
    memset(vis,0,sizeof(vis));
    scanf("%s%d",s,&n);
    for(i=0;i<n;i++)
    {
        scanf("%s",a[i]);
        if(a[i][0]==s[0]&&a[i][1]==s[1]||a[i][0]==s[1]&&a[i][1]==s[0])
            tag=1;
        if(s[0]==a[i][0])
            vis[0]=1;
        if(s[1]==a[i][1])
            vis[1]=1;
        if(s[0]==a[i][1])
            vis[2]=1;
        if(s[1]==a[i][0])
            vis[3]=1;
    }
    if(vis[2]&&vis[3])
        tag=1;
    if(tag)
    {
        printf("YES\n");
        return 0;
    }
    else
        printf("NO\n");
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值