zoukankan      html  css  js  c++  java
  • CodeForces 748B Santa Claus and Keyboard Check

    题意:给定两个字符串a和b,问有多少种不同的字母组合对,使得将这些字母对替换字符串b后,可以变成字符串a。注意字母对彼此各不相同。

    分析:vis[u]记录与u可形成关系的字母,若u与v不同,则形成字母对。若之后该关系被打破,则输出-1。

    #pragma comment(linker, "/STACK:102400000, 102400000")
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define Min(a, b) ((a < b) ? a : b)
    #define Max(a, b) ((a < b) ? b : a)
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 1000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    char a[MAXN], b[MAXN];
    const string s = "abcdefghijklmnopqrstuvwxyz";
    map<char, int> mp;
    int vis[30];
    int ans[30];
    void init(){
        for(int i = 0; i < 26; ++i){
            mp[s[i]] = i + 1;
        }
    }
    int main(){
        init();
        while(scanf("%s%s", a, b) == 2){
            memset(vis, 0, sizeof vis);
            memset(ans, 0, sizeof ans);
            int len = strlen(a);
            int cnt = 0;
            bool ok = true;
            for(int i = 0; i < len; ++i){
                int tmpa = mp[a[i]], tmpb = mp[b[i]];
                if(!vis[tmpa] && !vis[tmpb]){
                    vis[tmpa] = tmpb;
                    vis[tmpb] = tmpa;
                    if(tmpa != tmpb){
                        ans[cnt++] = tmpa;
                    }
                }
                else if(vis[tmpa] != tmpb || vis[tmpb] != tmpa){
                    ok = false;
                    break;
                }
            }
            if(!ok){
                printf("-1\n");
            }
            else{
                printf("%d\n", cnt);
                for(int i = 0; i < cnt; ++i){
                    printf("%c %c\n", ans[i] + 'a' - 1, vis[ans[i]] + 'a' - 1);
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    python 返回函数的使用
    你的服务器还在裸奔吗?
    云计算产品vSwitch原理
    网卡创建Bond
    UI自动化框架介绍
    常用底层linux命令
    Linux Bridge基本概念
    磁盘格式化及设置自动挂载
    Linux vi文本编辑器常用命令
    MySQL5.7安装方式
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6486382.html
Copyright © 2011-2022 走看看