前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >杭师校赛I题-Little Sub and Enigma AC代码

杭师校赛I题-Little Sub and Enigma AC代码

作者头像
wenzhuan
发布2022-08-15 12:34:13
2230
发布2022-08-15 12:34:13
举报
文章被收录于专栏:你会烧尽还是结冰

不难 听了出题人讲思路马上就会 但我就是要搞题解 纪念我的赛场自闭四小时

最坑的是25对推第26对 双向映射一一对应 直接数组存 感觉也许是输在英语不好?

题目:Little Sub and Enigma

Description 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. Input 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. Output 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. Author YE, Zicheng

放一下出题人,zjunb

AC代码

代码语言:javascript
复制
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<queue>
using namespace std;
int a[27], b[27];
string s1, s2;
int main() {
    while (cin >> s1 >> s2) {
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        int flag = 0;
        for (int i = 0; i < s1.length(); i++) {
            int t1, t2;
            t1 = s1[i] - 'a' + 1;
            t2 = s2[i] - 'a' + 1;
            if (!a[t1]) a[t1] = t2;
            else if (a[t1] != t2) {
                cout << "Impossible" << endl;
                flag = 1;
                break;
            }
            if (!b[t2]) b[t2] = t1;
            else if (b[t2] != t1) {
                cout << "Impossible" << endl;
                flag = 1;
                break;
            }
            //复读,双向存
        }
        if (flag) continue;
        //上面break的是for,还需要一个continue,晕了
        int ai, bi = 0;
        int cnt = 0;
        for (int i = 1; i <= 26; i++) {
            if (a[i] == 0) cnt++, ai = i;
            if (cnt >= 2) continue;
        }
        if (cnt == 1)
            for (int i = 1; i <= 26; i++)
                if (b[i] == 0) bi = i;
        if (bi) a[ai] = bi;//25推26
        for (int i = 1; i <= 26; i++) {
            if (a[i] ) {
                char tmp1, tmp2;
                tmp1 = i - 1 + 'a';
                tmp2 = a[i] - 1 + 'a';
                cout << tmp1 << "->" << tmp2 << endl;
            }
        }
    }
    return 0;
}

其他题莫得了 太菜了

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目:Little Sub and Enigma
  • AC代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档