1029. 旧键盘(20)
旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及实际被输入的文字,请你列出肯定坏掉的那些键。
输入格式:
输入在2行中分别给出应该输入的文字、以及实际被输入的文字。每段文字是不超过80个字符的串,由字母A-Z(包括大、小写)、数字0-9、以及下划线“_”(代表空格)组成。题目保证2个字符串均非空。
输出格式:
按照发现顺序,在一行中输出坏掉的键。其中英文字母只输出大写,每个坏键只输出一次。题目保证至少有1个坏键。
输入样例:7_This_is_a_test _hs_s_a_es输出样例:
7TI
分析:
处理问题的规模是考虑合适算法的出发点之一。这个题目中,一看到文字最多80个字符···那就直接暴力法了。用一个bool数组标记下哪个字母输出过,不要重复就好~
using System; namespace PAT { class Program { static void Main() { //字符串长度小,可以用暴力法直接做 string str1 = Console.ReadLine(); string str2 = Console.ReadLine(); bool[] flag_A = new bool[26]; bool[] flag_D = new bool[10]; bool flag = false; foreach(char ch in str1) { if(str2.IndexOf(ch) == -1) //字符串2中没有这个字符 { if (ch >= 'a' && ch <= 'z') { if(!flag_A[ch - 'a']) { flag_A[ch - 'a'] = true; Console.Write(ch.ToString().ToUpper()); } } else if(ch >='A' && ch <= 'Z') { if (!flag_A[ch - 'A']) { flag_A[ch - 'A'] = true; Console.Write(ch); } } else if (ch == '_') { if(!flag) { flag = true; Console.Write(ch); } } else { if (!flag_D[ch - '0']) { flag_D[ch - '0'] = true; Console.Write(ch); } } } } } } }
本文介绍了一种通过对比输入文本与输出文本的方法来确定键盘上损坏的按键。使用布尔数组跟踪损坏的键,并确保输出不重复。适用于字符数量不多的情况。

被折叠的 条评论
为什么被折叠?



