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

F. Spy-string

题目链接-F. Spy-string
在这里插入图片描述
在这里插入图片描述
题目大意
给定 n n n个长度相同的字符串,如果能构造出一个字符串,使得其于每个字符串不同的字母个数不大于 1 1 1,则输出这个字符串,否则输出 − 1 -1 1

解题思路
暴 力 暴力

  • 枚举每个字符串 m m m个位置的字母,每个位置都枚举一下 26 26 26个字母,用map记录原字符串和改变一个字母后的所有字符串出现的次数
  • 最后遍历map容器,判断是否有一个字符串的出现次数大于等于 n n n,若有则输出这个字符串,若没有则输出-1
  • 具体操作见代码

附上代码

#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=2e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
string s[15];
map<string,int> mp;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	int t;
	cin>>t;
	while(t--){
		int n,m;
		cin>>n>>m;
		mp.clear();
		for(int i=0;i<n;i++)
			cin>>s[i];
		for(int i=0;i<n;i++){
			mp[s[i]]++;//记录原字符串出现的次数
			for(int j=0;j<m;j++){
				for(char c='a';c<='z';c++){
					if(c!=s[i][j]){//若该位字母与将替换它的字母不一样
						char tmp=s[i][j];
						s[i][j]=c;//用c替换该位字母
						mp[s[i]]++;//记录新字符串出现的次数
						s[i][j]=tmp;//还原字符串
					}
				}
			}
		}
		bool ass=0;
		for(auto it:mp){
			if(it.second>=n){
				cout<<it.first<<endl;
				ass=1;
				break;
			}
		}
		if(!ass) cout<<-1<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值