干tm的字符串
题意:
给你两组字符串,对于第一组字符串,如果该字符串能通过某种置换以及排列成为第二个字符串,那么就可以称第二个字符串是第一个字符串的加密。
思路:(凯撒密码 + 排列密码)
本题应该正确理解这里“变换”的定义:比如:A可以变为B(字母表的下一个),但是不一定会变为B,可以是任何字母,也可以不变,依然是它本身。所以,相同的字母变换后的字母也一定相同,位置或许会变,但频率不会变。因此两个字符串的频率一致是关键。典型的错误思路就是先凯撒密码置换,再将密码排列。
#include <iostream> #include <cstring> #include <cstdio> #include <string> #include <algorithm> #include <queue> #include <set> using namespace std; typedef long long LL; typedef pair<int, int>PII; char str1[110], str2[110]; int cnt1[26], cnt2[26]; int main() { scanf("%s", str1); scanf("%s", str2); if (strlen(str1) != strlen(str2)) { printf("NO\n"); return 0; } int len = strlen(str1); for (int i = 0; i < len; i++) { cnt1[str1[i] - 'A'] ++; cnt2[str2[i] - 'A'] ++; } sort(cnt1, cnt1 + 26); sort(cnt2, cnt2 + 26); bool flag = true; for (int i = 0; i < 26; i++) if (cnt1[i] != cnt2[i]) { flag = false; break; } if (flag) printf("YES\n"); else printf("NO\n"); return 0; }
C - Message Decowding
题意:
第一行给你26个字母,分别对应了'a'、'b'...'z'。然后第二行给你一行加密过的字符串,让你还原成原文。
思路:
字符串用char一个字母一个字母的读入,并且边读入边输出。
注意点:
1)如果是大写字母要以相应的大写字母输出
2)非英文字母不做更改,直接输出。
#include <iostream> #include <cstring> #include <cstdio> #include <string> #include <algorithm> #include <queue> #include <set> using namespace std; typedef long long LL; typedef pair<int, int>PII; int main() { char key[26], ch; for (int i = 0; i < 26; i++) cin >> key[i]; getchar(); while (ch = getchar()) { if (ch == '\n') break; if (ch >= 'a' && ch <= 'z') cout << key[ch - 'a']; else if (ch >= 'A' && ch <= 'Z') cout << (char)(key[ch - 'A'] - 'a' + 'A');//注意转换为char不然会输出asch码 else cout << ch; } return 0; }
D - TEX Quotes
题意:
字符替换,遇到左单引号就换成 ``,遇到右单引号就换成 ' '
思路:
用getchar逐个字符输入,注意while里最后有个!=EOF,不然会输出超时,每次left取反就可以判断当前是左单引号还是右单引号
#include <iostream> #include <cstring> #include <cstdio> #include <string> #include <algorithm> #include <queue> #include <set> using namespace std; typedef long long LL; typedef pair<int, int>PII; int main() { char ch; bool left = true; while ((ch = getchar())!=EOF) { if (ch == '"') { if (left == true) cout << "``"; else cout << "''"; left = !left;//每次取反真的学到了 } else cout << ch; } return 0; }

浙公网安备 33010602011771号