题目描述

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.

Input

The first line contains an integer t (1≤t≤100) — the number of test cases. Then t test cases follow.
Each test case starts with a line containing two positive integers n (1≤n≤10) and m (1≤m≤10) — the number of strings and their length.
Then follow n strings ai, one per line. Each of them has length m and consists of lowercase English letters.

Output

Print t answers to the test cases. Each answer (if it exists) is a string of length m 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
5
2 4
abac
zbab
2 4
aaaa
bbbb
3 3
baa
aaa
aab
2 2
ab
bb
3 1
a
b
c
output
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.

题目大意

给你n个字符串,每个字符串长度为m,要求是否存在一个字符串使得该字符串与每个字符串的不同的字符个数小于等于1个。

题目分析

这道题就是暴力枚举每一种情况,然后验证是否合法即可。
其实就是考察写代码的能力。(比赛的时候我看出来是要暴力做了,但就是写不出来。。。)

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <map>
#include <unordered_map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
int const N=105;
int n,m;
string ans,s[15];
bool check()     //检查该情况是否合法
{for(int i=2;i<=m;i++)   //枚举其它m-1个字符串{int k=0;for(int j=0;j<n;j++){if(ans[j]!=s[i][j]) k++;   //统计不相同的字母数量if(k>=2) return false;     //大于2则不合法}}return true;    //否则合法
}
int main()
{int t;cin>>t;while(t--){cin>>m>>n;for(int i=1;i<=m;i++)cin>>s[i];bool st=false;for(int i=0;i<n;i++) //枚举每一位{ans=s[1];       //重置ans,保证每次只修改一个位置for(char j='a';j<='z';j++)   //枚举每一个字母{ans[i]=j;if(check()) {st=true; break;}  //检查是否合法,合法即可输出}if(st) break;}if(st) cout<<ans<<endl;   //如果有合法情况则输出else puts("-1");          //没有输出-1}return 0;
}

#644 (Div. 3)F. Spy-string(暴力枚举)相关推荐

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

    Codeforces Round #644 (Div. 3) F.Spy-string 题目链接 You are given n strings a1,a2,-,an: all of them hav ...

  2. Codeforces Round #359 (Div. 2) C. Robbers' watch 暴力枚举

    题目链接 题意是真的烦,到最后才知道是n个m其实就是限定表的两个时区的位数,所以所当数不够填满时区的时候前边自动补零 思路:首先来说不能有重复的数字的话,小时和分钟的总位数大于7肯定不行. 7的7次方 ...

  3. Codeforces Round #797 (Div. 3) F. Shifting String题解

    题意是给一个字符串s和一个与之相同长度的数组排列a,字符串s按照数组给定的位置进行变化,新字符串的第i个字符是上一个字符串的第a[i]个字符 比如 例子 5 ababa 2 1 4 5 3 变化6次 ...

  4. Codeforces Round #644(Div. 3) A-H

    A - Minimal Square 题意 给两个完全一样的矩形(平行且不重叠) 求能覆盖两个矩形的最小正方形的面积 思路 只有两种摆放方式 将两个矩形上下并列或者左右并列 得到的新图形 长或者宽是之 ...

  5. 枚举----暴力枚举

    第一题 题目描述 由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成. 比如: 210 x 6 = 1260 8 x 473 = 3784 27 x 81 = 2187 都符合要求. ...

  6. 算法小课堂(一)暴力枚举

    . 目录 一.概念 1.1相关概念 1.2应用场景 1.3局限性 二.相关问题 2.1例题1:统计 2.2例题二 二倍的问题 2.3例题3 奶牛碑文 2.4例四网友年龄 2.5例五生日年龄数 2.6数 ...

  7. codeforces数学1600day6[CodeForces - 1029C多区间交+枚举,CodeForces 992C[数学公式推导],CodeForces 992B[质因数分解+暴力枚举]]

    A - Maximal Intersection CodeForces - 1029C 题目大意:就是给你n个区间,这n个区间有公共的区间长度为x,现在叫你从这n个区间中删掉一个使得x最大化. 解题思 ...

  8. 最大字段和 冲出暴力枚举

    这篇解题报告是对我最近一些题的总结,里面的代码都是我解题,优化,再优化的过程的记录,记录了自己对算法的完善与优化思路,还有对编程哲学的理解:do it,do it well. 很感谢孙老师您,让自己可 ...

  9. C. Divisibility by Eight【暴力枚举】

    暴力枚举即可.枚举1位这种情况,枚举2位这种情况,枚举3位这种情况. 3位满足足以,其他的4位,5位...都包含1000必定满足. #include<bits/stdc++.h> usin ...

最新文章

  1. Linux 查看进程的命令
  2. MySQL 修改视图
  3. 无线网络技术基础 01
  4. 迟到的 cocoapod 版本适配 之网利宝
  5. WebService学习总结(3)——使用java JDK开发WebService
  6. matlab中lambertw,MATLAB解常微分方程
  7. 浅谈 C# CLR 执行模块
  8. python的三个基本数字类型_Python基础学习--三 基本数据类型
  9. 银行如何构建反欺诈模型
  10. 惠普HP CQ40 519TX XP系统安装以及XP驱动
  11. 极小极大搜索方法、负值最大算法和Alpha-Beta搜索方法
  12. NYOJ 137 取石子(三)(教主神题)
  13. 与电影同行的日子(同步更新)
  14. Web IDE落地全记录(一)
  15. 2014美国大学计算机专业排名,2014年美国大学计算机专业研究生排名
  16. 博客已死?移动互联网时代博客的价值
  17. Adobe Reader Acrobat Pro XI在连网下打开几秒后,卡顿并自动退出问题解决措施
  18. 实体店运营:能提高顾客留存率的店铺陈列方式
  19. 图片查看器 Viewer.js
  20. Dorado5学习笔记

热门文章

  1. openpyxl中的load_workbook()函数
  2. 软件分析与用户体验分析
  3. python中sep的用法:逗号的去除
  4. python 中的路径. ./ .. ../的区别
  5. operator framework搭建operator开发环境
  6. 计算机excel按F4是那个公式,excel中键盘F4到底怎么用?_excle 中的f4
  7. 【转】PHP乱码问题,UTF-8(乱码)
  8. php代码解决乱码问题
  9. C++中垃圾回收机制中几种经典的垃圾回收算法
  10. 如何用python一键去除图片、PDF水印?