zoukankan      html  css  js  c++  java
  • Swapping Characters CodeForces

    大意: 给定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 '
    '
    #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");
    }
    
  • 相关阅读:
    编写高质量Python代码的59个有效方法
    排序NB三人组(快速排序/堆排序/归并排序)
    排序之插入排序
    编写高质量代码 改善Python程序的91个建议——笔记(三)
    ImportHelper 导出帮助类
    使用Layer弹出一个页面
    MongoDB使用手册
    MVC4设置@Html.BeginForm的ID
    LayerManage
    Layer弹窗返回数据回调
  • 原文地址:https://www.cnblogs.com/uid001/p/10752574.html
Copyright © 2011-2022 走看看