Codeforces868A Bark to Unlock

45 篇文章 0 订阅
40 篇文章 0 订阅

标签:模拟

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

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

Input

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

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

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

Output

Print "YES" if Kashtanka can barkseveral 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, theanswer is "YES".

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

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

模拟大水题

Code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define mem(x,num) memset(x,num,sizeof x)
using namespace std;

int n;
char s[500],t[500];
char head[500],tail[500];

int main()
{
    cin>>s>>n;
	rep(i,1,n){
	    cin>>t;
		if((t[0]==s[0])&&(t[1]==s[1])){cout<<"YES\n";return 0;}
	    tail[t[0]]=true;
	    head[t[1]]=true;
	}
	if(head[s[0]]&&tail[s[1]]){cout<<"YES\n";return 0;}
	cout<<"NO\n";
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值