Swapping Characters CodeForces - 903E (字符串模拟)
大意: 给定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 (字符串模拟)的更多相关文章
- CodeForces 625B 字符串模拟+思维
题意 给出字符串a与b 可以将a中的单个字符改为# 问最少改多少次 a中就找不到b了 一开始想的是用strstr 因为如果找到 可以将strstr(a,b)-a+1改成# 即改首字母 用while循环 ...
- 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 ...
- 用字符串模拟两个大数相加——java实现
问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...
- HDU-3787(字符串模拟)
Problem Description 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开.现在请计算A+B的结果,并以正常形式输出. Input 输入包含 ...
- HDU-1002.大数相加(字符串模拟)
本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值. 本题思路:字符串模拟就能过,会Java的大佬应该不会点进来...... 参考代码: #include <cstdio&g ...
- HDU-Digital Roots(思维+大数字符串模拟)
The digital root of a positive integer is found by summing the digits of the integer. If the resulti ...
- Vigenère密码 2012年NOIP全国联赛提高组(字符串模拟)
P1079 Vigenère 密码 题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法――Vigenère 密 码.Vigenère 密码的加密解密算法简 ...
- CCF(JSON查询:40分):字符串+模拟
JSON查询 201709-3 纯字符串模拟,考的就是耐心和细心.可惜这两样我都缺... #include<iostream> #include<cstdio> #includ ...
- Codeforces 903E Swapping Characters
题目大意 考虑一个未知的长为 $n$($2\le n\le 5000$)由小写英文字母构成的字符串 $s$ .给出 $k$($1\le k\le 2500$,$nk\le 5000$)个字符串 $s_ ...
随机推荐
- 环形dp
对于环形的dp 大多情况都是破环成链 例如 那道宝石手镯的环形 一般来说都是要破环成链的. 或者说 是 两次dp 一次断开 一次强制连接即可. 我想 我应该沉淀下来了这些天写的题都有点虚 要不就是看了 ...
- mysql in 排序 也可以按in里面的顺序来排序
SQL: select * from table where id IN (3,9,6);这样的情况取出来后,其实,id还是按3,6,9,排序的,但如果我们真要按IN里面的顺序排序怎么办?SQL能不能 ...
- TCP/IP的分层管理
网络基础TCP/IP 我们通常所使用的网络(包括互联网)均是在TCP/IP协议族的基础上运作的.HTTP属于它内部的一个子集 TCP/IP协议族按层次分为:应用层,传输层,网络层和数据链路层(更好的划 ...
- 【摘】Fiddler工具使用介绍
摘自:https://www.cnblogs.com/miantest/p/7289694.html Fiddler基础知识 Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作 ...
- Apache ab并发负载压力测试(python+django+mysql+apache)
如标题,大家都知道秒杀中存在高并发使库存骤然为0,但在我们个人PC或小区域内是模拟不出这样的情景 现在利用 Apache ab并发负载压力测试 1,数据库建入库存字段并映射模型 2,view编写脚本 ...
- 内置委托func
1.p=>p.CTName,其中p是此委托入参,p.CTName是返回值 2. 3.调用委托的方法
- PHP Echarts Ajax Json柱形图示例
<?php $server = '127.0.0.1'; $user = 'root'; $password = ''; $database = 'yiibaidb'; $conn = new ...
- .NET 4.5 中新提供的压缩类(转载)
Windows8 的开发已经如火如荼开始了,在 Windows8 中提供的 .NET Framework 已经更新到了 4.5 版,其中又增加了一些新的特性,对压缩文件的支持就是其中之一. 在 4.5 ...
- hive sql执行的job在map时报java.lang.OutOfMemoryError的错误
较为详细且重要的一段报错信息是org.apache.hadoop.mapred.YarnChild: Error running child : java.lang.OutOfMemoryError: ...
- [转][访谈] Olivier Grisel谈scikit-learn和机器学习技术的未来
原文:http://www.csdn.net/article/2015-10-11/2825882 几周前,我们的Florian Douetteau (FD)对Olivier Grisel(OG)进行 ...