大意: 给定k个字符串, 长度均为n, 求是否存在一个串S, 使得k个字符串都可以由S恰好交换两个字符得到.

暴力枚举交换的两个字符的位置, 计算出交换后与其他串不同字符的个数, 若为1或>2显然不成立, 若为0必须要求存在两个相同的字符.

#include <iostream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif string s[N];
int n, k, vis[N], d[N];
map<int,int> q[N]; int main() {
cin>>k>>n;
REP(i,1,k) {
cin>>s[i];
for (auto t:s[i]) if (++q[i][t]>1) vis[i]=1;
if (q[i]!=q[1]) return puts("-1"),0;
}
if (k==1) {
swap(s[1][0],s[1][1]);
cout<<s[1]<<endl;
return 0;
}
REP(i,2,k) {
REP(j,0,n-1) d[i]+=s[1][j]!=s[i][j];
}
REP(i,0,n-1) REP(j,i+1,n-1) {
int ok = 1;
REP(kk,2,k) {
int t = d[kk];
d[kk] -= s[1][i]!=s[kk][i];
d[kk] -= s[1][j]!=s[kk][j];
d[kk] += s[1][j]!=s[kk][i];
d[kk] += s[1][i]!=s[kk][j];
if (d[kk]==1) ok = 0;
else if (d[kk]==0&&!vis[kk]) ok = 0;
else if (d[kk]>2) ok = 0;
d[kk] = t;
}
if (ok) {
swap(s[1][i],s[1][j]);
return cout<<s[1]<<endl,0;
}
}
puts("-1");
}

Swapping Characters CodeForces - 903E (字符串模拟)的更多相关文章

  1. CodeForces 625B 字符串模拟+思维

    题意 给出字符串a与b 可以将a中的单个字符改为# 问最少改多少次 a中就找不到b了 一开始想的是用strstr 因为如果找到 可以将strstr(a,b)-a+1改成# 即改首字母 用while循环 ...

  2. Codeforces Round #425 (Div. 2) B. Petya and Exam(字符串模拟 水)

    题目链接:http://codeforces.com/contest/832/problem/B B. Petya and Exam time limit per test 2 seconds mem ...

  3. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  4. HDU-3787(字符串模拟)

    Problem Description 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开.现在请计算A+B的结果,并以正常形式输出.   Input 输入包含 ...

  5. HDU-1002.大数相加(字符串模拟)

    本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值. 本题思路:字符串模拟就能过,会Java的大佬应该不会点进来...... 参考代码: #include <cstdio&g ...

  6. HDU-Digital Roots(思维+大数字符串模拟)

    The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...

  7. Vigenère密码 2012年NOIP全国联赛提高组(字符串模拟)

    P1079 Vigenère 密码 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简 ...

  8. CCF(JSON查询:40分):字符串+模拟

    JSON查询 201709-3 纯字符串模拟,考的就是耐心和细心.可惜这两样我都缺... #include<iostream> #include<cstdio> #includ ...

  9. Codeforces 903E Swapping Characters

    题目大意 考虑一个未知的长为 $n$($2\le n\le 5000$)由小写英文字母构成的字符串 $s$ .给出 $k$($1\le k\le 2500$,$nk\le 5000$)个字符串 $s_ ...

随机推荐

  1. MySQL 大表优化方案(长文)

    当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考以下步骤来优化: 单表优化 除非单表数据未来会一直不断上涨,否则不要一开始就考虑拆分,拆分会带来逻辑.部署.运维的各种复杂度,一般以整型 ...

  2. iptables 分析(1)

    原文:http://blog.chinaunix.net/uid-24207747-id-2622900.html iptables 是用户空间中用于管理包过滤及NAT 等的工具应用程序.它设置防火墙 ...

  3. git 远程删除文件

    git rm -r --cached a/2.txt //删除a目录下的2.txt文件 删除a目录 git rm -r --cached a git commit -m "删除a目录下的2. ...

  4. Hyper-v 虚拟机 关闭服务

    1.Win + R 运行 2.输入 services.msc 回车 3.禁用下图的服务

  5. byte转bit

    由于项目需要,传过来的数据是高位到低位的Byte,需要输出低位到高位的bool数组. public static bool[] getBits(byte[] byt) { bool[] ret = n ...

  6. js阻止表单默认提交、刷新页面

    一.阻止刷新页面 在表单中的提交按钮<button></button>标签改为<input type="button">或者在<butto ...

  7. 下载文件 utils

    package cn.itcast.bos.utils;   import java.io.IOException; import java.net.URLEncoder;   import sun. ...

  8. sql 身份证计算年龄和性别

    IdentityNumber 是身份证号 年龄: ,), GETDATE()) / 365.25) as '推荐人年龄', 15位的身份证计算年龄: case when b.IdentityNumbe ...

  9. 使用freemarker和itext把html转pdf

    1.把html转pdf,首先必须要解决中文显示问题,CSS样式问题以及可能的JS问题,先上例子,自己去体会. 2.先去下载simsun.ttc字体: 2.demo.html <!DOCTYPE ...

  10. linux系统(rpm与deb环境),JAVA JDK的配置

    步骤一:(配置 JAVA JDK  DEB系列linux系统) 1,下载JAVA JDK 1.1.官网下载java JDK (最好为1.7及以上版本) 下载地址http://www.oracle.co ...