【Codeforces Round #644 (Div. 3) F】Spy-string

题目链接

链接

翻译

让你构造一个和 \(n\) 个字符串都只有【最多一个地方】不同的字符串

题解

只考虑第一个字符串,假设第 \(i\) 个位置不同,那么每个位置都有 \(26\) 种可能(其中一种是和本身一样)

看看得到的字符串是不是符合要求的就好。

代码

#include <iostream>
#include <string>
using namespace std;

const int N = 10;

int T;
int n, m;
char s[N+10][N+10];

int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> T;
    while (T--){
        cin >> n >> m;
        for (int i = 1;i <= n; i++){
            cin >> (s[i] + 1);
        }
        bool ok = false;
        for (int i = 1;i <= m && !ok; i++){
            for (char key = 'a';key <= 'z' && !ok;key++){
                for (int j = 1;j <= m; j++){
                    s[0][j] = s[1][j];
                }
                s[0][i] = key;
                int ban = 0;
                for (int k = 1;k <= n; k++){
                    int dif = 0;
                    for (int l = 1;l <= m; l++){
                        if (s[0][l] != s[k][l]){
                            dif++;
                        }
                    }
                    if (dif > 1){
                        ban = 1;
                    }
                }
                if (!ban){
                    ok = true;
                    s[0][m+1] = '\0';
                    cout << (s[0]+1) << endl;
                }
            }
        }
        if (!ok){
            cout << "-1" << endl;
        }
    }
    return 0;
}
posted @ 2020-12-03 10:48  AWCXV  阅读(45)  评论(0编辑  收藏  举报