模拟

H. Delete Them
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Polycarp is a beginner programmer. He is studying how to use a command line.

Polycarp faced the following problem. There are n files in a directory and he needs to delete some of them. Polycarp wants to run a single delete command with filename pattern as an argument. All the files to be deleted should match the pattern and all other files shouldn't match the pattern.

Polycarp doesn't know about an asterisk '*', the only special character he knows is a question mark '?' which matches any single character. All other characters in the pattern match themselves only.

Formally, a pattern matches a filename if and only if they have equal lengths and all characters in the corresponding positions are equal except when the character in the pattern is '?', in which case the corresponding filename character does not matter.

For example, the filename pattern "a?ba?":

  • matches filenames "aabaa", "abba.", "a.ba9" and "a.ba.";
  • does not match filenames "aaba", "abaab", "aabaaa" and "aabaa.".

Help Polycarp find a pattern which matches files to be deleted and only them or report if there is no such pattern.

Input

The first line of the input contains two integers n and m (1 ≤ m ≤ n ≤ 100) — the total number of files and the number of files to be deleted.

The following n lines contain filenames, single filename per line. All filenames are non-empty strings containing only lowercase English letters, digits and dots ('.'). The length of each filename doesn't exceed 100. It is guaranteed that all filenames are distinct.

The last line of the input contains m distinct integer numbers in ascending order a1, a2, ..., am (1 ≤ ai ≤ n) — indices of files to be deleted. All files are indexed from 1 to n in order of their appearance in the input.

Output

If the required pattern exists, print "Yes" in the first line of the output. The second line should contain the required pattern. If there are multiple solutions, print any of them.

If the required pattern doesn't exist, print the only line containing "No".

Examples
input
3 2
ab
ac
cd
1 2
output
Yes
a?
input
5 3
test
tezt
test.
.est
tes.
1 4 5
output
Yes
?es?
input
4 4
a
b
c
dd
1 2 3 4
output
No
input
6 3
.svn
.git
....
...
..
.
1 2 3
output
Yes
.???

题意:给你n个字符串,你需要找出一个pattern串,这个字符串如果跟n个字符串中的一个匹配,就要删除该字符串,两个字符串匹配是指对应字符相等或者pattern串中该位置为“?”,题目要求只删除给定字符串而不删除其他字符串,如果找不到这样的一个pattern串就输出no,反之输出yes和该字符串。

解题思路:直接模拟即可,先找出它要求删除的字符串,比较他们的字符串长度,如果不相等直接输出no,因为不可能找到这样一个字符串了,如果所有的字符串长度相等,那么接下来先找出字符串,从每个字符串的第一位开始,如果所有要删除的字符串的这一位都相等,那么pattern串的这一位直接置为这一个字符,反之,置为“?”,找完pattern串之后,还要用找出的pattern串与其他不需要删除的字符串比较,只有pattern串与它们都不匹配才输出yes,并且输出pattern串,反之输出no。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
string s[maxn];
int n,m;
int a[maxn];
bool d[maxn];
int len;
char pattern[maxn];
bool ok(string x)
{
    for(int i = 0; i < len; i++)
    {
        if(pattern[i] != '?'&&pattern[i] != x[i])
        return false;
    }
    return true;
}
int main()
{
    scanf("%d%d",&n,&m);
    bool ans = true;
    memset(d,false,sizeof(d));
    for(int i = 1; i <= n; i++)
    {
        cin>>s[i];
    }
    for(int i = 1; i <= m; i++)
    {
        scanf("%d",&a[i]);
        d[a[i]] = true;
    }
    len = s[a[1]].size();
    for(int i = 2; i <= m; i++)
    {
        if(s[a[i]].size() != len)
        {
            ans = false;
            break;
        }
    }
    if(ans)
    {
        bool flag;
        bool res = false;
        for(int i = 0; i < len; i++)
        {
            flag = false;
            char term = s[a[1]][i];
            for(int j = 2; j <= m; j++)
            {
                if(s[a[j]][i] != term)
                {
                    pattern[i] = '?';
                    flag = true;
                    break;
                }
            }
            if(!flag) pattern[i] = term;
        }
        for(int i = 1; i <= n; i++)
        {
            if(s[i].size()==len&&!d[i])
            {
                if(ok(s[i]))
                {
                    res = true;
                    break;
                }
            }
        }
        if(res) printf("No\n");
        else
        {
            printf("Yes\n");
            for(int i = 0; i < len; i++)
            {
                printf("%c",pattern[i]);
            }
            printf("\n");
        }
    }
    else
    {
        printf("No\n");
    }
    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值