构造思维 Codeforces Round #644 (Div. 3) F. Spy-string

F. Spy-string

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

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

Note that the desired string s may be equal to one of the given strings ai, 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.

题目大意:
给你n个长为m的字符串,构造一个字符串s,这个字符串长度为m,并且与给的n个字符串分别最多相差一个字符;


几乎告诉你是构造题,但是怎么构造呢?

直接改动第一个字符串的一个字母,因为要保证每个字符串都符合条件,所以第一个字符串也要符合,既然它要符合,那么就以它改动一个字母后的字符串作为答案判断就行;

代码:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=1100;
const int M=2000000;
const LL mod=1e9+7;
string s[20];
int n,m;
bool judge(string p){
	for(int i=1;i<=n;i++){
		int sum=0;
		for(int j=0;j<m;j++){
			if(s[i][j]!=p[j]) sum++;
		}
		if(sum>=2) return false;
	}
	return true;
}
int main(){
	int t;cin>>t;
	while(t--){
		cin>>n>>m;
		for(int i=1;i<=n;i++) cin>>s[i];
		int ok=0;
		for(int i=0;i<m;i++){
			if(ok) break;
			for(int j=0;j<=25;j++){
				string t=s[1];
				t[i]='a'+j;
				if(judge(t)){
					ok=1;
					cout<<t<<endl;
					break;
				}
			}
		}
		if(!ok) cout<<-1<<endl; 
	}
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值