Hack 掉自己的代码
查看原帖
Hack 掉自己的代码
648550
eoinlee楼主2023/11/2 14:50
#include <string>
#include <iostream>
#include <unordered_map>
using namespace std;

string a, b, c;
unordered_map<char, char> cd;
unordered_map<char, char> dc;

int main() {
   ios::sync_with_stdio(false);
   cin.tie(nullptr);

   cin >> a >> b >> c;

   if (a.length() < 26)
      return puts("Failed") && 0;

   int len = a.length();
   for (int i = 0; i < a.length(); i++) {
      if (dc.count(b[i]) && dc[b[i]] != a[i])
         return puts("Failed") && 0;
      cd[a[i]] = b[i];
      dc[b[i]] = a[i];
   }

   for (char ch : c)
      putchar(cd[ch]);

   putchar('\n');
   return 0;
}

以上是我自己写的一份错误代码,因为它没有判断长度等于或超过 26 个字母的明文是否包含所有的字母。所以,该代码无法通过以下数据:

输入

AAAAAAAAAAAAAAAAAAAAAAAAAA
BBBBBBBBBBBBBBBBBBBBBBBBBB
C

正确输出

Failed

实际输出

(空)

但是该代码奇迹般地通过了本题的所有数据。故请求加入 Hack 数据,或者其实增强一下数据也可以。

上述代码应该加入以下这段才能通过本数据:

for (int i = 'A'; i <= 'Z'; i++)
   if (!cd.count(i) || !dc.count(i))
      return puts("Failed") && 0;
2023/11/2 14:50
加载中...