zoukankan      html  css  js  c++  java
  • Delete Them

    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
    Copy
    3 2
    ab
    ac
    cd
    1 2
    Output
    Copy
    Yes
    a?
    Input
    Copy
    5 3
    test
    tezt
    test.
    .est
    tes.
    1 4 5
    Output
    Copy
    Yes
    ?es?
    Input
    Copy
    4 4
    a
    b
    c
    dd
    1 2 3 4
    Output
    Copy
    No
    Input
    Copy
    6 3
    .svn
    .git
    ....
    ...
    ..
    .
    1 2 3
    Output
    Copy
    Yes
    .???

    注意一下表达式不能表达该组外的单词,所以有了check()
     1 #pragma warning(disable:4996)
     2 #include<map>
     3 #include<queue>
     4 #include<string>
     5 #include<cstdio>
     6 #include<cstring>
     7 #include<iostream>
     8 #include<algorithm>
     9 using namespace std;
    10 
    11 int n, m;
    12 int x[105], use[105];
    13 
    14 bool check(string s1,string s2) {
    15     int p = s1.size();
    16     int q = s2.size();
    17     if (p != q) return true;
    18     for (int i = 0; i < p; i++) {
    19         if (s2[i] == '?') continue;
    20         else if (s2[i] != s1[i]) return true;
    21     }
    22     return false;
    23 }
    24 
    25 int main()
    26 {
    27     while (cin >> n >> m) {
    28         memset(use, 0, sizeof(use));
    29 
    30         string a[105];
    31         for (int i = 1; i <= n; i++) cin >> a[i];
    32 
    33         int k;
    34         for (int i = 1; i <= m; i++) {
    35             cin >> x[i];
    36             use[x[i]] = 1;
    37             k = a[x[i]].size();
    38         }
    39 
    40         bool flag = true;
    41         for (int i = 1; i <= m; i++) if (a[x[i]].size() != k) { flag = false; break; }
    42 
    43         if (!flag) cout << "No" << endl;
    44         else {
    45             string ans = "";
    46             for (int i = 0; i < k; i++) {
    47                 bool reflag = true;
    48                 for (int j = 2; j <= m; j++) {
    49                     if (a[x[j]][i] != a[x[j - 1]][i]) reflag = false;
    50                 }
    51                 if (!reflag) {
    52                     ans += '?';
    53                 }
    54                 else {
    55                     ans += a[x[1]][i];
    56                 }
    57             }
    58 
    59             flag = true;
    60             for (int i = 1; i <= n; i++) {
    61                 if (use[i]) continue;
    62                 if (check(a[i], ans)) continue;
    63                 flag = false;
    64                 break;
    65             }
    66             
    67             if (!flag) cout << "No" << endl;
    68             else {
    69                 cout << "Yes" << endl;
    70                 for (int i = 0; i < k; i++) cout << ans[i];
    71                 cout << endl;
    72             }
    73         }
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    工作中常用的工具
    lua 调试、热重载
    Lua库收集
    Mac常见端口
    Swift 学习笔记1
    项目-微博(模仿新浪微博)
    iOS多线程
    iOSCoreData介绍
    iOS数据库操作流程
    iOS中数据库运用之前的准备-简单的数据库
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/9022298.html
Copyright © 2011-2022 走看看