问题

题解
复建第一题,被坑惨了。理解题意很重要,不要看中文翻译:如果之后出现重复字母,则错误次数+1而不是失败。
大致意思就是在字符串1中找到是否有字符串2中的字母,最多可以找错7次。
成功:没猜单词==0
失败:没猜单词>0 && 所剩机会==0
放弃:!成功 && !失败
可以自然想到哈希表,用alpha[x]表示字母x是否出现过。
技巧是如果这个字母已经出现过,则直接把**alpha[x] = false**,解决重复出现字母的问题。
然后是输入问题,没有给出字符串长度,考虑都是对字母做处理可以直接读取字符。(不过好像并没有在这个地方设坑….)
当然可以用std::set实现。
AC代码
/*模拟 哈希成功:没猜单词==0失败:没猜单词>0 && 所剩机会==0放弃:!成功 && !失败*/#include <iostream>#include <cstdio>#define MAXN 30int main(){int rnd = 0, left = 0, chance = 7;bool win = 0, lose = 0;bool alpha[MAXN];while (scanf("%d", &rnd) && rnd != -1){getchar();// 初始化for (int i = 0;i < MAXN;i++)alpha[i] = 0;left = 0;chance = 7;win = lose = 0;char ch;// 处理计算机给出字符串while ((ch=getchar()) != '\n')if (!alpha[ch - 97]){alpha[ch - 97] = 1;++left;}// 处理所猜字符串while ((ch = getchar()) != '\n'){if (win || lose) continue;//保证字符串读取完整if (alpha[ch - 97]) --left;else --chance;alpha[ch - 97] = 0;if (!chance) lose = 1;if (!left) win = 1;}// 判断结果printf("Round %d\n", rnd);if (win) printf("You win.\n");else if (lose) printf("You lose.\n");else printf("You chickened out.\n");}return 0;}
