0
点赞
收藏
分享

微信扫一扫

1029 旧键盘(附详细注释,逻辑分析)


写在前面

  • 实现思路
  • 字符串数组
  • 2层循环遍历串a不在串b的字符,并打印输出。已打印过标记true
  • 题目较简单,10分钟内a题(时间略长)

测试用例

input:
7_This_is_a_test
_hs_s_a_es

output:
7TI

ac代码

  • 算法笔记(基础版)

#include <cstring>
#include <iostream>
using namespace std;

int main()
{
char str1[100], str2[100];
bool HashTable[128] = {false};
scanf("%s %s", str1, str2);

int len1 = strlen(str1);
int len2 = strlen(str2);
for(int i=0; i<len1; i++)
{
int j;
char c1, c2;
for(j=0; j<len2; j++)
{
c1 = str1[i];
c2 = str2[j];
if(c1 >= 'a' && c1<='z') c1-=32;
if(c2 >= 'a' && c2<='z') c2-=32;
if(c1==c2) break;
}

if(j==len2 && HashTable[c1]==false)
{
printf("%c", c1);
HashTable[c1] = true;
}
}
return 0;
}

  • 另类尝试(unordered_map)
  • 实现思路
  • unordered_map封装字符,并循环标识
  • 循环打印损坏键,并更新标识

#include <iostream>
#include <cstring>
#include <unordered_map>
using namespace std;
int main()
{
unordered_map<char, int> ans;
char a[100], b[100];
scanf("%s %s", a, b);
int lena = strlen(a),lenb = strlen(b);
for(int i=0; i<lena; i++) ans[toupper(a[i])] = 0;
for(int i=0; i<lenb; i++) ans[toupper(b[i])] = 1;
// 输出无序
// for(unordered_map<char, int>::iterator iter=ans.begin(); iter!=ans.end(); iter++)
// {
// if(iter->second<=0)
// cout << iter->first;
// }
for(int i=0; i<lena; i++)
{
if(ans[toupper(a[i])]<=0)
{
printf("%c", toupper(a[i]));
ans[toupper(a[i])] = 1;
}
}
return 0;
}

学习代码

  • ​​实现思路···推荐···​​
  • 字符串查找函数
  • 结果字符转大写拼接

#include <iostream>
#include <cctype>
using namespace std;
int main() {
string s1, s2, ans;
cin >> s1 >> s2;
for (int i = 0; i < s1.length(); i++)
if (s2.find(s1[i]) == string::npos && ans.find(toupper(s1[i])) == string::npos)
ans += toupper(s1[i]);
cout << ans;
return 0;
}

知识点小结

unordered_map<char, int> ans;
toupper(a[i]);
s2.find(s1[i]) == string::npos
for(unordered_map<char, int>::iterator iter=ans.begin(); iter!=ans.end(); iter++)


举报

相关推荐

0 条评论