zoukankan      html  css  js  c++  java
  • Little Sub and Enigma

    Little Sub builds a naive Enigma machine of his own. It can only be used to encrypt/decrypt lower-case letters by giving each letter a unique corresponding lower-case letter. In order to ensure the accuracy, no contradiction or controversy is allowed in both the decryption and the encryption, which means all lower-case letters can only be decrypted/encrypted into a distinct lower-case letter.
    Now we give you a string and its encrypted version. Please calculate all existing corresponding relationship which can be observed or deducted through the given information.

    输入

    The first line contains a string S, indicating the original message.
    The second line contains a string T, indicating the encrypted version.
    The length of S and T will be the same and not exceed 1000000.

    输出

    we use a string like ’x->y’ to indicate that letter x will be encrypted to letter y.
    Please output all possible relationships in the given format in the alphabet order.
    However, if there exists any contradiction in the given information, please just output Impossible in one line.

    样例输入

    复制样例数据

    banana
    cdfdfd
    

    样例输出

    a->d
    b->c
    n->f
    

    ps:25组确定后,第26组也会确定。

    #include <iostream>
    #include <bits/stdc++.h>
    using namespace std;
    int a1[200];
    int a2[200];
    int main()
    {
        string s1,s2;
        cin>>s1>>s2;
        if(s1.length()!=s2.length()) {
            printf("Impossible
    ");
            return 0;
        }
        int cnt=0,t=0;
        for(int i=0;i<s1.length();i++){
            int x=s1[i];
            int y=s2[i];
            if(!a1[x]&&!a2[y]) {
                a1[x]=y;
                cnt++;
                a2[y]=1;
            }
            else if(a1[x]&&a1[x]==y) continue;
            else {
                t=1;
                break;
            }
        }
        if(t) printf("Impossible
    ");
        else {
            if(cnt==25){
                int p=-1,q=-1;
                for(int i='a';i<='z';i++){
                    if(!a1[i]) p=i;
                    if(!a2[i]) q=i;
                }
                for(int i='a';i<'z';i++){
                    if(i==p) printf("%c->%c
    ",i,q);
                    else printf("%c->%c
    ",i,a1[i]);
                }
                return 0;
            }
            for(int i='a';i<='z';i++){
                if(a1[i]) printf("%c->%c
    ",i,a1[i]);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    Codeforces Round #131 (Div. 2) E. Relay Race dp
    Codeforces Round #130 (Div. 2) C
    Codeforces Round #130 (Div. 2) A. Dubstep
    UVA 11858 Frosh Week 逆序对统计
    UVA 11149 Power of Matrix 快速幂
    UVA 12382 Grid of Lamps 贪心
    POJ 3614 Sunscreen 贪心
    Codeforces Round #238 (Div. 2) D. Toy Sum 暴搜
    Codeforces Round #227 (Div. 2) E. George and Cards 线段树+set
    Codeforces Round #327 (Div. 2) E. Three States
  • 原文地址:https://www.cnblogs.com/skyleafcoder/p/12319501.html
Copyright © 2011-2022 走看看