• F. Spy-string


    You are given nn strings a1,a2,,ana1,a2,…,an: all of them have the same length mm. The strings consist of lowercase English letters.

    Find any string ss of length mm such that each of the given nn strings differs from ss in at most one position. Formally, for each given string aiai, there is no more than one position jj such that ai[j]s[j]ai[j]≠s[j].

    Note that the desired string ss may be equal to one of the given strings aiai, or it may differ from all the given strings.

    For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first.

    Input

    The first line contains an integer tt (1t1001≤t≤100) — the number of test cases. Then tt test cases follow.

    Each test case starts with a line containing two positive integers nn (1n101≤n≤10) and mm (1m101≤m≤10) — the number of strings and their length.

    Then follow nn strings aiai, one per line. Each of them has length mm and consists of lowercase English letters.

    Output

    Print tt answers to the test cases. Each answer (if it exists) is a string of length mm consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print "-1" ("minus one", without quotes).

    Example
    input
    Copy
    5
    2 4
    abac
    zbab
    2 4
    aaaa
    bbbb
    3 3
    baa
    aaa
    aab
    2 2
    ab
    bb
    3 1
    a
    b
    c
    
    output
    Copy
    abab
    -1
    aaa
    ab
    z
    Note

    The first test case was explained in the statement.

    In the second test case, the answer does not exist.

     刚开始居然去想着用DFS,傻逼了

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    #define ll              long long
    #define pii             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define forn(i, n)      for(int i = 0; i < int(n); i++)
    using namespace std;
    int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod = 1e9 + 7;
    const int N = 3e3 + 5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    bool prime(int x) {
        if (x < 2) return false;
        for (int i = 2; i * i <= x; ++i) {
            if (x % i == 0) return false;
        }
        return true;
    }
    ll qpow(ll m, ll k, ll mod)
    {
        ll res = 1, t = m;
        while (k)
        {
            if (k & 1)
                res = res * t % mod;
            t = t * t % mod;
            k >>= 1;
        }
        return res;
    }
    bool check(string s,string s1)
    {
        int cnt = 0;
        forn(i, s.size())
        {
            if (s[i] != s1[i])
                cnt++;
        }
        if (cnt > 1)
            return false;
        return true;
    }
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            int n, m;
            cin >> n >> m;
            vector<string> a(n);
            forn(i, n)
                cin >> a[i];
            string res;
            forn(i, m)
            {
                string s = a[0];
                rep(j, 0, 25)
                {
                    int fg = 0;
                    s[i] = j + 'a';
                    forn(k, n)
                    {
                        if (!check(s, a[k]))
                            fg = 1;
                    }
                    if (!fg)
                    {
                        res = s;
                        break;
                    }
                }
            }
            if (res.length() != m)
                cout << -1 << endl;
            else
                cout << res << endl;
        }
        return 0;
    }
  • 相关阅读:
    【翻译】让你的网站飞起来
    理解ASP.NET MVC中的模型绑定
    【转载】创建定制ASP.NET MVC视图引擎
    修改STM32主频
    Cortex系列ARM核心及体系结构介绍
    递归
    NFD模拟兴趣包的转发
    NX 笔记
    MicroPython 8266 配置
    Python JSON操作
  • 原文地址:https://www.cnblogs.com/dealer/p/13184518.html
Copyright © 2020-2023  润新知