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

    Swapping Characters

    题解:

    先算出其他串和第一个串的字母个数是否相同。

    再算出其他串和第一个串不同的字母位置个数。

    然后枚举第一个串交换的位置。

    计算交换之后的不同字母的位置个数。

    如果个数为0,则至少有2个相同的字母。

    如果个数为2,则说明交换那2个位置之后可以相同。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
    #define LL long long
    #define ULL unsigned LL
    #define fi first
    #define se second
    #define pb push_back
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define lch(x) tr[x].son[0]
    #define rch(x) tr[x].son[1]
    #define max3(a,b,c) max(a,max(b,c))
    #define min3(a,b,c) min(a,min(b,c))
    typedef pair<int,int> pll;
    const int inf = 0x3f3f3f3f;
    const int _inf = 0xc0c0c0c0;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const LL _INF = 0xc0c0c0c0c0c0c0c0;
    const LL mod =  (int)1e9+7;
    const int N = 3e4;
    string s[N];
    int dif[N];
    int num[26], num2[26];
    int main(){
        int k, n;
        scanf("%d%d", &k, &n);
        for(int i = 1; i <= k; ++i)
            cin >> s[i];
        int f = 0;
        for(int i = 0; i < n; ++i)
            ++num[s[1][i]-'a'];
        for(int i = 0; i < 26; ++i)
            f += (num[i] > 1);
        for(int i = 2; i <= k; ++i){
            for(int j = 0; j < n; ++j){
                ++num2[s[i][j]-'a'];
                dif[i] += (s[i][j] != s[1][j]);
            }
            for(int j = 0; j < 26; ++j){
                if(num[j] != num2[j]){
                    puts("-1");
                    return 0;
                }
                num2[j] = 0;
            }
        }
        for(int i = 0; i < n; ++i){
            for(int j = i+1; j < n; ++j){
                int ok = 1;
                for(int z = 2; z <= k; ++z){
                    int cnt = dif[z];
                    cnt -= (s[1][i] != s[z][i]);
                    cnt -= (s[1][j] != s[z][j]);
                    cnt += (s[1][j] != s[z][i]);
                    cnt += (s[1][i] != s[z][j]);
                    if((!cnt && f) || cnt == 2) ok++;
                }
                if(ok == k){
                    swap(s[1][i], s[1][j]);
                    cout << s[1] << endl;
                    return 0;
                }
            }
        }
        puts("-1");
        return 0;
    }
    View Code
  • 相关阅读:
    《观止》读后感
    读产品经理相关书籍有感
    windows phone 7基础点随手记
    Windows phone 7画图画字
    《CLR via C#》读书笔记
    《Beginning C# Objcets》学习笔记
    ASP.NET 使用alert弹出对话框后,CSS样式失效,字体变大的解决方法
    .NET COOKIE /SESSION/CACHE操作类
    【原创】VS2005 Web应用程序打包并安装数据库
    存储过程实现多条件查询
  • 原文地址:https://www.cnblogs.com/MingSD/p/11112274.html
Copyright © 2011-2022 走看看