大意: 给定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. LeetCode 696 Count Binary Substrings 解题报告

    题目要求 Give a string s, count the number of non-empty (contiguous) substrings that have the same numbe ...

  2. C++11 vector使用emplace_back代替push_back

    C++11中,针对顺序容器(如vector.deque.list),新标准引入了三个新成员:emplace_front.emplace和emplace_back,这些操作构造而不是拷贝元素.这些操作分 ...

  3. 流控制、FlowControl

    这个选项基本上所有网卡都会有,但是叫法会有些差别,比如Realtek网卡叫做流控制,Intel网卡叫做流程控制,还有一些网卡选项干脆是英文的,叫做FlowControl,很多交换机上也有这个功能,也叫 ...

  4. python frist lesson

    1.print("") 代表打印字符段 2.name2 = name ,然后改变name的赋值,name2的值还是以前name的值,说明name2指向的是内存中name的赋值. 3 ...

  5. python-16

    #知识点一.函数的作用域 int() #内置变量 B v = 6 #全局变量G def outer(): x = 5 #嵌套变量 E def inner(): b = 10 #局部变量 L print ...

  6. Linux LVS_NAT DR

    一.lvs-nat LVS是Linux Virtual Server的简写,意即Linux虚拟服务器.是由章文嵩博士开发的一个在内核层面的负载均衡调度器.    lvs是在netfilter的INPU ...

  7. java解答:有17个人围成一圈(编号0~16),从第0号的人开始从1报数,凡报到3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个人为止,问此人原来的位置是多少号?

    package ttt; import java.util.HashMap; import java.util.Map.Entry; /** * 有17个人围成一圈(编号0~16),从第0号的人开始从 ...

  8. n皇后问题——关于斜线的编号

    题目大意:在n*n的棋盘中,放置n个皇后(同一行.同一列.同一斜线,只有一个皇后) 这道题是一道非常经典的dfs模板题,同一行.同一列的判断不是很难,但同一斜线有一定的难度,下面给出关于斜线编号的解决 ...

  9. Ajax跨域请求COOKIE无法带上的解决办法

    原生ajax请求方式: var xhr = new XMLHttpRequest(); xhr.open("POST", "http://xxxx.com/demo/b/ ...

  10. sqlserver 空 处理

    NULL:没有对(列)变量输入数据 'NULL'字符串:是长度为4的字符串 空字符串:有对(列)变量输入数据,输入的数据为空字符串 select case lxdh when '' then '空' ...