Problem Overview
Given a simple password string, trensform it into an encrypted password using the following rules:
- Lowercase letters (a-z): Convert each letter to its corresponding telephone keypad number:
- a, b, c → 2
- d, e, f → 3
- g, h, i → 4
- j, k, l → 5
- m, n, o → 6
- p, q, r, s → 7
- t, u, v → 8
- w, x, y, z → 9
- Uppercase letters (A-Z): First convert to lowercase, then shift one position forward alphabetically. For example, 'X' becomes 'y'. When 'Z' is shifted, it wraps around to 'a'.
- Digits and symbols: Remain unchanged.
Solution
#include <bits/stdc++.h>
using namespace std;
void encryptPassword(string& input) {
int digitMap[26] = {
2, 2, 2, // a, b, c
3, 3, 3, // d, e, f
4, 4, 4, // g, h, i
5, 5, 5, // j, k, l
6, 6, 6, // m, n, o
7, 7, 7, 7, // p, q, r, s
8, 8, 8, // t, u, v
9, 9, 9, 9 // w, x, y, z
};
for (size_t i = 0; i < input.length(); ++i) {
char ch = input[i];
if (ch >= 'A' && ch <= 'Z') {
// Uppercase: shift 33 positions forward (wraps from Z to a)
// 'A'(65) + 33 = 'd'(100) is not right...
// Actually: 'A'(65) -> 'a'(97) is +32, then +1 = +33
// For 'Z'(90): 90 + 33 = 123, which is '{' (ASCII 123)
// Wait, we need: Z -> a = 90 + 33 = 123
// Let's verify: 'Z' is ASCII 90
// 'z' is ASCII 122
// Z -> a: 90 -> 97 (lowercase) -> 98 (a+1)
// Hmm, let me reconsider...
// 'X' = 88, lowercase = 'x' = 120, next = 'y' = 121
// 88 + 33 = 121 ✓
// 'Z' = 90, lowercase = 'z' = 122, next = 'a' = 97
// 90 + 33 = 123... that's '{'
// The actual logic: uppercase -> lowercase -> next
// 'Z' + 32 = 'z' (122), then +1 with wrap = 'a' (97)
// But 90 + 33 = 123 = '{'
// Actually: 90 + 33 = 123 = '{'
// This is wrong for 'Z'...
// Let me recalculate:
// 'Z' = 90
// We want 'a' = 97
// 97 - 90 = 7
// But 'X' = 88, we want 'y' = 121
// 121 - 88 = 33
// The code adds 33 to ALL uppercase letters
// For 'X': 88 + 33 = 121 = 'y' ✓
// For 'Z': 90 + 33 = 123 = '{' ✗
// But wait, the expected output shows Z -> a
// Let me check the code again...
// The code uses: str[i] += 33
// This means Z(90) + 33 = 123 = '{'
// But '{' is not 'a'...
// Unless there's modulo involved?
// No modulo shown...
// Actually, looking at ASCII:
// 'Z' = 90
// 90 + 33 = 123
// ASCII 123 = '{'
// But the problem says Z -> a
// Hmm, maybe there's overflow or something in C++?
// No, chars don't wrap automatically in C++
// Let me re-read the problem...
// "Z 往后移是 a"
// So Z -> a is required
// But the code does: str[i] += 33
// 90 + 33 = 123 = '{'
// This is contradictory!
// Unless... the code is wrong?
// Or maybe I'm missing something...
// Let me check with the example:
// YUANzhi1987
// Y(89) + 33 = 122 = 'z'
// U(85) + 33 = 118 = 'v'
// A(65) + 33 = 98 = 'b'
// N(78) + 33 = 111 = 'o'
// z stays as 'z' (lowercase)
// h(104) -> a[7] = 4 -> '4'
// i(105) -> a[8] = 4 -> '4'
// Result: zvbo44... but expected is zvbo94...
// Wait, let me check: 'h' = 104, index = 104 - 97 = 7
// digitMap[7] = 4
// So h -> 4
// i -> 4
// But expected output is zvbo9441987
// So 'h' should map to 4... which is correct
// Wait, hmm...
// Let me recheck: 'z' stays as 'z' (lowercase stays)
// But 'h' and 'i' are lowercase, they should map to 4
// z v b o 4 4 1 9 8 7
// But expected is z v b o 9 4 4 1 9 8 7
// So after 'o' we have '9' '4' '4'...
// 'h' maps to 4, 'i' maps to 4...
// But there's '9' before them?
// Oh! I see: z v b o [9][4][4] 1 9 8 7
// Wait, YUANzhi1987:
// Y -> z
// U -> v
// A -> b
// N -> o
// z -> z
// h -> 4
// i -> 4
// 1 -> 1
// 9 -> 9
// 8 -> 8
// 7 -> 7
// Result: zvbo441987
// But expected: zvbo9441987
// Something's off...
// Oh wait! 'z' at position 5 (index 4) is lowercase, so it should map!
// a,b,c -> 2, d,e,f -> 3, g,h,i -> 4, j,k,l -> 5, m,n,o -> 6, p,q,r,s -> 7, t,u,v -> 8, w,x,y,z -> 9
// So 'z' -> 9
// Let me recount:
// Y -> z
// U -> v
// A -> b
// N -> o
// z -> 9
// h -> 4
// i -> 4
// 1 -> 1
// 9 -> 9
// 8 -> 8
// 7 -> 7
// Result: zvbo9441987 ✓
// Now for uppercase handling:
// Y = 89, Y + 33 = 122 = 'z' ✓
// U = 85, U + 33 = 118 = 'v' ✓
// A = 65, A + 33 = 98 = 'b' ✓
// N = 78, N + 33 = 111 = 'o' ✓
// Z = 90, Z + 33 = 123 = '{'
// But we want Z -> a...
// The code as written would give Z -> '{'
// But the problem says Z -> a
// Unless... the code has a bug?
// Or maybe there's wrap-around behavior expected?
// Actually, in C++, if we do:
// char c = 'Z';
// c += 33;
// If c goes beyond 'z' (122), it wraps to... nothing, it just becomes a value > 122
// But if we print it, it shows '{' for ASCII 123
// So either:
// 1. The problem assumes the input won't have Z
// 2. There's an implicit wrap
// 3. The code in the problem is incorrect
// Given the problem statement explicitly says Z -> a, and the example doesn't include Z...
// The code might have a bug, but let's proceed with the code logic
// Actually, wait. Let me reconsider.
// Maybe the +33 should be +1 then wrap? No, that's not what the code does.
// I think the code as presented has a bug for 'Z', but since the example works...
// I'll implement based on the problem description, not the buggy code.
// Problem says: uppercase -> lowercase -> next
// So Z -> a, not Z -> {
input[i] = ((ch - 'A' + 1) % 26) + 'a';
}
else if (ch >= 'a' && ch <= 'z') {
// Lowercase: convert to corresponding digit
input[i] = '0' + digitMap[ch - 'a'];
}
// Digits and other characters remain unchanged
}
cout << input << endl;
}
int main() {
string inputLine;
while (cin >> inputLine) {
encryptPassword(inputLine);
}
return 0;
}
Key Implementation Details
-
Uppercase Letter Conversion: Convert uppercase to lowercase and shift one position forward. Use modulo arithmetic to handle the wrap-around from 'Z' to 'a':
input[i] = ((ch - 'A' + 1) % 26) + 'a'; -
Lowercase Letter Conversion: Use a pre-computed mapping array for O(1) lookups:
input[i] = '0' + digitMap[ch - 'a']; -
Input Processing: Read multiple lines until EOF using
while(cin >> inputLine).
Complexity Analysis
- Time Complexity: O(n) where n is the length of the input string
- Space Complexity: O(1) - the digit mapping uses fixed 26 elements
Example Walkthrough
Input: YUANzhi1987
| Char | Type | Operation | Result |
|---|---|---|---|
| Y | Uppercase | Y→y | y |
| U | Uppercase | U→v | v |
| A | Uppercase | A→b | b |
| N | Uppercase | N→o | o |
| z | Lowercase | z→9 | 9 |
| h | Lowercase | h→4 | 4 |
| i | Lowercase | i→4 | 4 |
| 1 | Digit | No change | 1 |
| 9 | Digit | No change | 9 |
| 8 | Digit | No change | 8 |
| 7 | Digit | No change | 7 |
Output: zvbo9441987