题解 | #字符串加密#
字符串加密
https://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include <cctype> #include <iostream> #include <set> #include <vector> using namespace std; int main() { string s1, s2, res; cin >> s1; cin >> s2; int n1 = s1.size(), n2 = s2.size(); vector<char> arr(26); set<char> se; int loc = 0; for (int i = 0; i < n1; i++) { if ( se.find(s1[i]) == se.end()) { se.insert( s1[i] ); arr[loc++] = s1[i]; } } for (int i = 0; i < 26; i++) { if ( se.find('a' + i) == se.end()) { se.insert( 'a' + i ); arr[loc++] = 'a' + i; } } for (int i = 0; i < n2; i++) { if (s2[i] >= 'A' && s2[i] <= 'Z') { char tmp = tolower(s2[i]); res += toupper(arr[tmp - 'a']); } else { res += arr[s2[i] - 'a']; } } cout << res << endl; return 0; } // 64 位输出请用 printf("%lld")
这个题巧妙的地方在于用一个简单的数组实现了对应关系。