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

骑猪看日落 2022-06-07 01:58 241阅读 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

  1. ya
  2. 4
  3. ah
  4. oy
  5. to
  6. ha

output

  1. YES

input

  1. hp
  2. 2
  3. ht
  4. tp

output

  1. NO

input

  1. ah
  2. 1
  3. ha

output

  1. 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,否则就看一个的第二个和另一个的第一个是否拼接可以成该串

暂时通过的代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<stdio.h>
  4. #include<math.h>
  5. #include<string>
  6. #include<stdio.h>
  7. #include<queue>
  8. #include<stack>
  9. #include<map>
  10. #include<vector>
  11. #include<deque>
  12. #include<algorithm>
  13. #define ll long long
  14. #define INF 1008611111
  15. #define M (t[k].l+t[k].r)/2
  16. #define lson k*2
  17. #define rson k*2+1
  18. using namespace std;
  19. int main()
  20. {
  21. int i,j,n,tag=0;
  22. char s[10],a[105][10];
  23. int vis[10];
  24. memset(vis,0,sizeof(vis));
  25. scanf("%s%d",s,&n);
  26. for(i=0;i<n;i++)
  27. {
  28. scanf("%s",a[i]);
  29. if(a[i][0]==s[0]&&a[i][1]==s[1]||a[i][0]==s[1]&&a[i][1]==s[0])
  30. tag=1;
  31. if(s[0]==a[i][0])
  32. vis[0]=1;
  33. if(s[1]==a[i][1])
  34. vis[1]=1;
  35. if(s[0]==a[i][1])
  36. vis[2]=1;
  37. if(s[1]==a[i][0])
  38. vis[3]=1;
  39. }
  40. if(vis[2]&&vis[3])
  41. tag=1;
  42. if(tag)
  43. {
  44. printf("YES\n");
  45. return 0;
  46. }
  47. else
  48. printf("NO\n");
  49. return 0;
  50. }

发表评论

表情:
评论列表 (有 0 条评论,241人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Codeforces Round #663 (Div. 2) A

    [题目链接][Link 1] 这是一道简单题,我当时构造了一种剩余最大,剩余最小,轮番输出的方式,来实现了题目中的要求。 实际上好像还有更简单的方式,直接从大到小输出,或者

    相关 Codeforces Round #556 (Div. 2) A

    题面很简单,思路就是简单贪心,si数组是贮存购买数组,bi数组是贮存出售数组,题面是给你r的货币,让你通过出售和购买来获取最大价值,第一种算法是通过找出bi数组最大值,和si数