PAT-乙-1029 1029 旧键盘 (20 分)

代码

#include <iostream>

using namespace std;

int main(){
	
	string s1, s2;
	cin>>s1>>s2;
	
	//equals to java: toUpperCase()
	for(int i=0; i<s1.length(); i++){
		if(s1.at(i)>='a' && s1.at(i)<='z'){
			s1[i] = s1[i]-32;
		}
	}
	for(int i=0; i<s2.length(); i++){
		if(s2.at(i)>='a' && s2.at(i)<='z'){
			s2[i] = s2[i]-32;
		}
	}
	
	char ch[129] = {0};
	for(int i=0; i<s2.length(); i++){
		if(ch[s2.at(i)]==0){
			ch[s2.at(i)] = 1;
		}
	}
	
	for(int i=0; i<s1.length(); i++){
		if(ch[s1.at(i)]==0){
			//guarantee one letter only output once
			ch[s1.at(i)] = -1;
			cout<<s1.at(i);
		}
	}
	cout<<endl;
	
	return 0;
} 

注解

由于只输出大写,因此首先应转换成大写。然后利用Hash的思想,找哪些是坏键。如何每个坏键只输出一次,且按照输入顺序依次输出?解决方法是输出后赋值-1,避免再次输出。遍历时按照输入顺序遍历。

结果

PAT-乙-1029 1029 旧键盘 (20 分)

相关文章:

  • 2021-08-29
  • 2021-04-16
  • 2022-01-21
  • 2022-12-23
  • 2021-11-17
  • 2021-11-17
猜你喜欢
  • 2021-11-15
  • 2021-08-19
  • 2022-12-23
  • 2021-07-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案