Codeforces Round #644 (Div. 3) F.Spy-string (暴力出奇迹。。。。)

题目传送
题意:
给你n个长度为m的字符串,问你是否能构造一个字符串,使得与这n个字符串最多有1个字符不同。如果不能输出-1.
(1 <= n <= 10 && 1 <= m <= 10)

思路:
真就暴力出奇迹。。因为这个数据实在是太小了。那么我们枚举所有的可能(先以这n个字符串中的任意一个为基准枚举)(因为最多有1个字符串不同,我们把这个字符串的每一个位置从‘a’遍历到‘z’就可以了)与这n个字符串比较就行了。

AC代码

#include <bits/stdc++.h>
inline int read(){char c = getchar();int x = 0,s = 1;
while(c < '0' || c > '9') {if(c == '-') s = -1;c = getchar();}
while(c >= '0' && c <= '9') {x = x*10 + c -'0';c = getchar();}
return x*s;}
using namespace std;
#define NewNode (TreeNode *)malloc(sizeof(TreeNode))
#define Mem(a,b) memset(a,b,sizeof(a))
const int N = 1e6 + 5;
const long long INFINF = 0x7f7f7f7f7f7f7f;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-7;
const unsigned long long mod = 998244353;
const double II = acos(-1);
const double PP = (II*1.0)/(180.00);
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> piil;
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t,n,m;
    cin >> t;
    while(t--)
    {
        string s[15];
        cin >> n >> m;
        for(int i = 0;i < n;i++) cin >> s[i];
        string str = s[0],ss = "";//让str字符串变成第一个字符串,再枚举,ss记录答案
        for(int i = 0;i < m;i++)//枚举每个位置
        {
            int ans = 1;
            string a = str;
            for(char j = 'a';j <= 'z';j++)//从‘a’枚举到‘z’
            {
                a[i] = j;
                for(int k = 1;k < n;k++)//与其他的字符串比较
                {
                    int sum = 0;
                    for(int q = 0;q < m;q++)
                        if(a[q] != s[k][q])
                            sum++;
                    if(sum >= 2) ans = 0;//如果有俩个以上的字符不相同了,则这个字符串不可能是答案
                }
                if(ans) ss = a;//满足答案条件,记录
                ans = 1;//初始化
            }
        }
        ss == "" ? cout << -1 << endl : cout << ss << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值