您的位置:首页 > 其它

CodeForces 868

2017-10-21 13:30 363 查看
CodeForces 868A

Bark to Unlock

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).

Example

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.

题意:给一个目标串(只有两个字母),然后下面n个串中随便选组成一个新串,然后看能否在这个新串里找到和目标串相同的子串。

水。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
int main()
{
char s[4],a,b;
int m=0,n=0,i,j,k;
scanf("%s",s);
a=s[0];
b=s[1];
scanf("%d",&k);
for(i=1; i<=k; i++)
{
scanf("%s",s);
if(s[0]==b)
m=1;
if(s[1]==a)
n=1;
if(s[0]==a&&s[1]==b)
m=n=1;
}
if(m==1&&n==1)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}


CodeForces 868B

Race Against Time

Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a single problem has been prepared yet? Misha had. And this time he has a really strong excuse: he faced a space-time paradox! Space and time replaced each other.

The entire universe turned into an enormous clock face with three hands — hour, minute, and second. Time froze, and clocks now show the time h hours, m minutes, s seconds.

Last time Misha talked with the coordinator at t1 o’clock, so now he stands on the number t1 on the clock face. The contest should be ready by t2 o’clock. In the terms of paradox it means that Misha has to go to number t2 somehow. Note that he doesn’t have to move forward only: in these circumstances time has no direction.

Clock hands are very long, and Misha cannot get round them. He also cannot step over as it leads to the collapse of space-time. That is, if hour clock points 12 and Misha stands at 11 then he cannot move to 1 along the top arc. He has to follow all the way round the clock center (of course, if there are no other hands on his way).

Given the hands’ positions, t1, and t2, find if Misha can prepare the contest on time (or should we say on space?). That is, find if he can move from t1 to t2 by the clock face.

Input

Five integers h, m, s, t1, t2 (1 ≤ h ≤ 12, 0 ≤ m, s ≤ 59, 1 ≤ t1, t2 ≤ 12, t1 ≠ t2).

Misha’s position and the target time do not coincide with the position of any hand.

Output

Print “YES” (quotes for clarity), if Misha can prepare the contest on time, and “NO” otherwise.

You can print each character either upper- or lowercase (“YeS” and “yes” are valid when the answer is “YES”).

Example

Input

12 30 45 3 11

Output

NO

Input

12 0 1 12 1

Output

YES

Input

3 47 0 4 9

Output

YES

Note

The three examples are shown on the pictures below from left to right. The starting position of Misha is shown with green, the ending position is shown with pink. Note that the positions of the hands on the pictures are not exact, but are close to the exact and the answer is the same.



题意:h,m,s,t1,t2分别代表时针分针秒针,起始位置,终止位置。时针分针秒针分别指向三个位置,现在问能否从t1走到t2,条件是中间没有三个指针挡住路,可以逆时针,也可顺时针走。

吐槽。。。这个题的细节真是哔了狗了。。。。。。。

注释都在代码里

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<double,int>mp;//用map来标记时针,分针,秒针
int main()
{
int h,m,s,t1,t2;
cin>>h>>m>>s>>t1>>t2;
double hh,mm,ss;
hh=(double)(h*5==60?0:h*5);//全都化为60单位的,如果指针只在12处,就让它为0
mm=(double)m;
ss=(double)s;
if(mm!=0||ss!=0)
hh+=0.5;//分针或者秒针不在0处,时针+0.5个刻度
if(ss!=0)
mm+=0.5;//秒针不在0处,分针加0.5个刻度
mp[hh]++;
mp[mm]++;
mp[ss]++;//用map把时针,分针,秒针各标记一下
t1=t1*5==60?0:t1*5;
t2=t2*5==60?0:t2*5;
if(t1>t2)//把小的放在前面便于循环
swap(t1,t2);
int flag1=0,flag2=0;
for(double i=t1; i!=t2; i+=0.5)//顺时针方向遍历
{
if(i==60)//当i等于60的时候,变为0
i=0;
if(mp[i]&&i!=t1&&i!=t2)//i!=t1&&i!=t2判断是否初始位置或者终止位置是否在指针上
{
flag1=1;
break;
}
}
for(double i=t1; i!=t2; i-=0.5)//逆时针方向遍历
{
if(mp[i]&&i!=t1&&i!=t2)//这个判断要放在下面if的前面,防止有指向0的指针,但是没有判断
{
flag2=1;
break;
}
if(i==0)
i=59.5;
}
if(!flag1||!flag2)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: