A1084

本题可以从两个不同角度思考:
1、我的想法:第二个字符串是按照第一个字符串的顺序敲出来的,只不过因为键盘老旧,有些键无法输出,那么,可以将两个字符串str1,str2分别从前往后比较,若相等,则各向后移一位;若不等,则str1向后移一位,判断不相等的这个字符之前是否已经输出过,若已输出过,则将其忽略,若没输出过,则输出之。最后,要么两个字符串共同遍历到末尾;要么str2遍历到了末尾,str1还剩下几个无法呈现在str2上的字符,需额外处理之。

#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int hashes(char c){
		if(c>='0'&&c<='9')return c-'0';
		else if(c=='_')return 99;
		else if(c>='A'&&c<='Z')return c-'0';
		else if(c>='a'&&c<='z')return c-'0'-32;
	}
int main(){
	#ifdef ONLINE_JUDGE
	#else
		freopen("1.txt","r",stdin);
	#endif
	bool flag[100];                                 //记录某个特定字符是否已被打印过了 
	string orig,type;
	getline(cin,orig);
	getline(cin,type);
	int i=0,j;
	for(j=0;j<type.length();){                      //一次遍历 
		if(orig[i]!=type[j]){
			int convert=hashes(orig[i]);
			if(flag[convert]==false){
				if(orig[i]>='a'&&orig[i]<='z'){
					printf("%c",orig[i]-32);
				}else{
					printf("%c",orig[i]);
				}
				flag[convert]=true;
			}
			i++;
		}else{
			i++;
			j++;
		}
	}
	while(i<orig.length()){                               //type遍历到了末尾,orig还剩下几个无法呈现在type上的字符,需额外处理之
			int convert=hashes(orig[i]);
			if(flag[convert]==false){
				if(orig[i]>='a'&&orig[i]<='z'){
					printf("%c",orig[i]-32);
				}else{
					printf("%c",orig[i]);
				}
				flag[convert]=true;
			}
			i++;
	}
	return 0;
}

2、算法笔记的思路:因键盘老旧无法输出的字符,在str2中不可能出现,因此,可将str1从头至尾遍历,每遍历一个字符,就将str2遍历一遍,寻找是否有相等的字符,若没有,则是我们要找的字符,然后再判断该字符是否已被输出过了。

#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main(){
	#ifdef ONLINE_JUDGE
	#else
		freopen("1.txt","r",stdin);
	#endif
	char str1[100],str2[100];
	scanf("%s",str1);
	scanf("%s",str2);
	bool hashTable[128];
	for(int i=0;i<strlen(str1);i++){
		int j;
		char c1,c2;
		c1=str1[i];
		if(c1>='a'&&c1<='z')c1=c1-32;
		for(j=0;j<strlen(str2);j++){
			c2=str2[j];
			if(c2>='a'&&c2<='z')c2=c2-32;
			if(c1==c2)break;
		}
		if(j==strlen(str2)&&hashTable[c1]==false){
			hashTable[c1]=true;
			printf("%c",c1);
		}
	}
	return 0;
}

注:下面代码与上面的唯一的区别,上面用的字符数组,下面用的字符串,结果下面的通过不了

#include<cstdio>
#include<cstdlib>
#include<string.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main(){
	#ifdef ONLINE_JUDGE
	#else
		freopen("1.txt","r",stdin);
	#endif
	string str1,str2;
	getline(cin,str1);
	getline(cin,str2);
	bool hashTable[128];
	for(int i=0;i<str1.length();i++){
		int j;
		char c1,c2;
		c1=str1[i];
		if(c1>='a'&&c1<='z')c1=c1-32;
		for(j=0;j<str2.length();j++){
			c2=str2[j];
			if(c2>='a'&&c2<='z')c2=c2-32;
			if(c1==c2)break;
		}
		if(j==str2.length()&&hashTable[c1]==false){
			hashTable[c1]=true;
			printf("%c",c1);
		}
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章