Gym - 100735H

 

Informikas was cleaning his drawers while he found a toy of his childhood. Well, it's not just a toy, it's a bunch of cubes with letters and digits written on them.

Informikas remembers that he could have make any word he could think of using these cubes. He is not sure about that now, because some of the cubes have been lost.

Informikas has already come up with a word he would like to make. Could you help him by saying if the word can be built from the cubes in the drawer?

Input

On the first line of input there is a string S, consisting of lowercase English letters, and an integer N (4 ≤ |S| ≤ 201 ≤ N ≤ 100) – the word Informikas want to build and the number of cubes. On the every of the following N lines there are 6 characters. Every of those characters is either a lowercase English letter or a digit.

It is guaranteed that the string S consists only of lowercase English letters.

Output

Output one word, either "YES", if the word can be built using given cubes, or "NO" otherwise.

Example

Input

dogs 4d 1 w e 7 9o 2 h a v eg 3 c o o ks 3 i e s 5

Output

YES

Input

banana 6b a 7 8 9 1n 1 7 7 7 6a 9 6 3 7 8n 8 2 4 7 9a 7 8 9 1 3s 7 1 1 2 7

Output

NO

题意是:找一下是否能组成INPUT给的单词  由于是正方体  所以只能左右 上下找。
#include <bits/stdc++.h>
using namespace std;


#define LL long long
#define INF 1E4 * 1E9
#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
const int maxn=1e3+5;
const int maxx=1e6+5;

/*
dogs 4
d 1 w e 7 9
o 2 h a v e
g 3 c o o k
s 3 i e s 5

banana 6
b a 7 8 9 1
n 1 7 7 7 6
a 9 6 3 7 8
n 8 2 4 7 9
a 7 8 9 1 3
s 7 1 1 2 7
*/
struct node
{
    int x,y;
}q[maxn];

char cnt[maxn],cnl[maxn][maxn];
int n,vis[maxn],ans,len;

void dfs()
{
  if(ans==len) return;
  for(int i=0;i<n;i++)//每一排
  {
    if(vis[i]) continue;
    for(int j=0;j<6;j++)//每一排的字符数
    {
      if(cnl[i][j]==cnt[ans])
      {
        ans++;vis[i]=1;
        if(len==ans) return;
        dfs();
        if(len==ans) return;
        ans--;vis[i]=0;break;
      }
    }
  }
}
int main()
{
    cin>>cnt>>n;
    int num=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<6;j++)
        {
            cin>>cnl[i][j];
            if(cnl[i][j]==cnt[0])//如果满足INPUT给的单词的第一个字母  则加入结构体中存起来
           {
              q[num].x=i;
              q[num].y=j;
              num++;
           }
        }
    }
    me(vis);
    len=strlen(cnt);
    for(int i=0;i<num;i++)
    {
        vis[q[i].x]=1;
        ans=1; //ans的作用是已经符合了多少个单词  比如dogs 如果ans=4 则已经满足条件
        dfs();
        if(len==ans)//这里就是上面所说
        {
            puts("YES");
            return 0;

        }
        vis[q[i].x]=0;
        ans=0;
    }
    puts("NO");
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值