2016-2017 ACM-ICPC, NEERC, Southern Subregional Contest H. Delete Them

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
.???
/*
有一个坑,就是全是?的情况:虽然想删除的没有一个能匹配的,但是只要长度和其他的不一样就行了
*/
#include<bits/stdc++.h>
#define N 110
using namespace std;
char str[N][N];
int op[N];
int visit[N];
int visit2[N];
vector<string> v1,v2;
int main()
{
    //freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
    int n,m;
    memset(visit,0,sizeof visit);
    memset(visit2,0,sizeof visit2);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%s",&str[i]);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&op[i]);
        visit2[op[i]]=1;
    }    
    int flag=1;
    for(int i=1;i<m;i++)
    {
        if(strlen(str[op[i]])!=strlen(str[op[i+1]]))
            flag=0;
        //cout<<strlen(str[op[i]])<<" "<<strlen(str[op[i+1]])<<endl;
    }
    if(!flag)
    {
        puts("No");
        return 0;
    }
    //cout<<"ok"<<endl;
    int len=strlen(str[op[1]]);
    int cur=0;
    for(int i=0;i<len;i++)
    {
        int f=1;
        for(int j=1;j<m;j++)
        {
            if(str[op[j]][i]!=str[op[j+1]][i])
            {
                f=0;
                break;
            }    
            //cout<<str[op[j]][i]<<""<<str[op[j+1]][i]<<endl;
        }
        if(f)
        {
            //cout<<"ok"<<endl;
            visit[i]=1;
            cur++;
        }    
    }
    if(!cur)
    {
        int F=0;
        for(int i=1;i<=n;i++)
        {
            if(strlen(str[i])==strlen(str[op[1]])&&!visit2[i])
            {
                F++;
            }
        }
        if(!F)
        {
            puts("Yes");
            for(int i=0;i<len;i++)
                printf("?");
            printf("
");
        }
        else
            puts("No");
        return 0;
    }
    else
    {
        for(int i=1;i<=n;i++)
        {
            if(strlen(str[i])==len&&!visit2[i])
            {
                int cur1=0;
                for(int j=0;j<len;j++)
                {
                    if(visit[j]&&str[i][j]==str[op[1]][j])
                    {
                        //cout<<i<<endl;
                        cur1++;
                    }
                }
                if(cur1==cur)
                {
                    //cout<<"ok"<<endl;
                    puts("No");
                    return 0;
                }
            }
        }
        puts("Yes");
        for(int i=0;i<len;i++)
        {
            if(visit[i])
                printf("%c",str[op[1]][i]);
            else
                printf("?");
        }
        printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5991088.html