XM13. 密码破译
描述
我们来做一个简单的密码破译游戏。破译的规则很简单,将数字转换为字母,1转化为a,2转化为b,依此类推,26转化为z。现在输入的密码是一串数字,输出的破译结果是该数字串通过转换规则所能产生的所有字符串。输入描述
多行数据,每行为一个数字串。输出描述
多行数据,每行对应输出通过数字串破译得到的所有字符串,并按照字符串顺序排列,字符串之间用单个空格分隔。每行开头和结尾不允许有多余的空格。示例1
输入:
1 12 123
输出:
a ab l abc aw lc
C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-05-23
#include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> const int MAXV = 1010; int n, cnt; char password[MAXV], ans[MAXV]; int a[MAXV]; void DFS(int x, int index) { if(x == n) { if(cnt) printf(" "); ans[index] = '\0'; printf("%s", ans); cnt++; } if(a[x] == 0) return; if(a[x] >= 3 || x == n - 1 || (a[x] == 2 && a[x + 1] > 6)) { ans[index] = 'a' + a[x] - 1; DFS(x + 1, index + 1); } else { ans[index] = a[x] - 1 + 'a'; DFS(x + 1, index + 1); ans[index] = 10 * a[x]+a[x+1] - 1 + 'a'; DFS(x + 2, index + 1); } } int main() { while(scanf("%s", password) != EOF) { n = strlen(password); for(int i = 0; i < n; i++) a[i] = password[i] - '0'; cnt = 0; DFS(0, 0); printf("\n"); } return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 376KB, 提交时间: 2020-05-20
#include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> const int MAXV = 1010; int n, cnt; char password[MAXV], ans[MAXV]; int a[MAXV]; void DFS(int x, int index) { if(x == n) { if(cnt) printf(" "); ans[index] = '\0'; printf("%s", ans); cnt++; } if(a[x] == 0) return; if(a[x] >= 3 || x == n - 1 || (a[x] == 2 && a[x + 1] > 6)) { ans[index] = 'a' + a[x] - 1; DFS(x + 1, index + 1); } else { ans[index] = a[x] - 1 + 'a'; DFS(x + 1, index + 1); ans[index] = 10 * a[x]+a[x+1] - 1 + 'a'; DFS(x + 2, index + 1); } } int main() { //freopen("input.txt", "r", stdin); while(scanf("%s", password) != EOF) { n = strlen(password); for(int i = 0; i < n; i++) a[i] = password[i] - '0'; cnt = 0; DFS(0, 0); printf("\n"); } return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 380KB, 提交时间: 2020-05-14
#include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> const int MAXV = 1010; int n, cnt; char password[MAXV], ans[MAXV]; int a[MAXV]; void DFS(int x, int index) { if(x == n) { if(cnt) printf(" "); ans[index] = '\0'; printf("%s", ans); cnt++; } if(a[x] == 0) return; if(a[x] >= 3 || x == n - 1 || (a[x] == 2 && a[x + 1] > 6)) { ans[index] = 'a' + a[x] - 1; DFS(x + 1, index + 1); } else { ans[index] = a[x] - 1 + 'a'; DFS(x + 1, index + 1); ans[index] = 10 * a[x]+a[x+1] - 1 + 'a'; DFS(x + 2, index + 1); } } int main() { //freopen("input.txt", "r", stdin); while(scanf("%s", password) != EOF) { n = strlen(password); for(int i = 0; i < n; i++) a[i] = password[i] - '0'; cnt = 0; DFS(0, 0); printf("\n"); } return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 472KB, 提交时间: 2020-05-16
#include<stdio.h> #include<math.h> #include<stdlib.h> #include<string.h> const int MAXV = 1010; int n, cnt; char password[MAXV], ans[MAXV]; int a[MAXV]; void DFS(int x, int index) { if(x == n) { if(cnt) printf(" "); ans[index] = '\0'; printf("%s", ans); cnt++; } if(a[x] == 0) return; if(a[x] >= 3 || x == n - 1 || (a[x] == 2 && a[x + 1] > 6)) { ans[index] = 'a' + a[x] - 1; DFS(x + 1, index + 1); } else { ans[index] = a[x] - 1 + 'a'; DFS(x + 1, index + 1); ans[index] = 10 * a[x]+a[x+1] - 1 + 'a'; DFS(x + 2, index + 1); } } int main() { //freopen("input.txt", "r", stdin); while(scanf("%s", password) != EOF) { n = strlen(password); for(int i = 0; i < n; i++) a[i] = password[i] - '0'; cnt = 0; DFS(0, 0); printf("\n"); } return 0; }
C++ 解法, 执行用时: 2ms, 内存消耗: 480KB, 提交时间: 2021-03-13
#include <iostream> #include <cstring> #include <cmath> using namespace std; string ans = ""; string s; int len =0; void dfs (int end){ if (end == len){ cout<<ans<<" "; return ; } int no1 =(int)(s[end]-'0'); int no2 =(int)(s[end]-'0')*10+(int)(s[end+1]-'0'); if (no1) { char t = 'a'+no1-1; ans+=t; dfs (end+1); ans=ans.substr(0,ans.length()-1); } if (no2>9&&no2<=26&&end+2<=len){ char t = 'a'+no2-1; ans+=t; dfs (end+2); ans=ans.substr(0,ans.length()-1); } } int main (){ while (cin>>s){ len = s.length(); dfs(0); cout<<endl; } }