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_ ...
随机推荐
- PrimeNG之DataTable
--数据表显示在表格格式数据. Basic Import import {DataTableModule,SharedModule} from 'primeng/primeng'; source &l ...
- TypeScript笔记
#安装typescript [1] npm install -g typescript #编译typescript tsc test.ts //会生成test.js文件 #泛型,即使用“类型变量”,函 ...
- nvm 安装使用
事先说明-------先安装nvm,再安装nodejs [nvm参考安装地址] nvm install 6.9.4 # 安装nodejs6.9.4版本 nvm use 6.9.4 # 使用nodejs ...
- Django进阶之QuerySet和中介模型
QuerySet QuerySet是查询集,就是传到服务器上的url里面的查询内容.其形态类似于Python的列表,列表中的元素是QuerySet对象.支持大部分列表的内置方法. 可切片 QueryS ...
- slf4j日志使用
scala中 trait LogSupport { protected val log = LoggerFactory.getLogger(this.getClass) } 需要要到的类 extend ...
- python笔记-正则表达式常用函数
1.re.findall()函数 语法:re.findall(pattern,string,flags=0) --> list(列表) 列出字符串中模式的所有匹配项,并作为一个列表返回.如果无匹 ...
- Java命令行参数解析
参考 http://blog.csdn.net/mldxs/article/details/36204079 http://rensanning.iteye.com/blog/2161201 imp ...
- fastjson java类、字符串、jsonObject之前的转换
json对象转成json字符串 JSONObject json = new JSONObject(); json.put("page",1); json.put("pag ...
- nuxtJs中直接使用自带的@nuxtjs/axios
最初我以为在nuxtjs中是需要重新npm install axios,但是其实nuxtjs自己集成了这个数据渲染方法 你只需在nuxt.config.js中配置一下就可以了 modules: [ / ...
- 安装caffe-ssd或者caffe时make all,make pycaffe,make test运行慢的问题
之所有运行慢,是因为没有在这三条语句后面加上 -j,即没用全部的进程运行,不加-j 表示用单一进程运行,加上-j5表示用5个进程,-j不带数字表示用所有进程