Codeforces868a bark to unlock

阅读全文大概需要 2分钟
本文总阅读量
Posted by yjjr's blog on February 6, 2018

标签:模拟

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 barkndistinct 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 integern(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;
}



本文可以转载,但必须附上原文链接,否则你会终生找不到妹子!!!欢迎关注我的CSDN: ahyjjr