题意:417 - Word Index
题意:每个字符串按题目中那样去映射成一个数字,输入字符串,输出数字
思路:这题还是比较水的,由于一共只有83000多个数字,所以对应一个个数字去映射就可以了,注意字符串进位的情况处理即可
代码:
#include <stdio.h>
#include <string.h>
#include <map>
#include <string>
using namespace std;
char str[10];
map<string, int> idx;
void init() {
memset(str, 0, sizeof(str));
str[0] = 'a'; idx[str] = 1;
for (int i = 2; i <= 83681; i++) {
for (int j = 4; j >= 0; j--) {
if (str[j] != 0) {
if (str[j] != 'z') str[j]++;
else {
int k = j;
while (k >= 0) {
if ('z' - str[k] == j - k)
k--;
else break;
}
if (k != -1) {
str[k]++;
for (int l = k + 1; l <= j; l++) {
str[l] = str[l - 1] + 1;
}
}
else {
for (int l = 0; l <= j + 1; l++)
str[l] = 'a' + l;
}
}
idx[str] = i;
break;
}
}
}
}
int main() {
init();
char s[10];
while (~scanf("%s", s)) {
if (idx.count(s))
printf("%d\n", idx[s]);
else
printf("0\n");
}
return 0;
}