其实代码复杂度还是蛮高的,不过,数据范围小呀~~嘿嘿,主要用了set
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
char str1[88];
char str2[88];
set<char> s1,s2;
set<char> dif;
char upper(char c){
char ans=c;
if (c<='z'&&c>='a') {
ans=c-'a'+'A';
}
return ans;
}
void insert(set <char> &s,char str[15]){
char temp;
for (int i=0; str[i]!='\0'; i++) {
temp=upper(str[i]);
s.insert(temp);
}
}
int main(){
//freopen("/Users/pantingting/Documents/code/data/input", "r", stdin);
scanf("%s",str1);
insert(s1,str1);
scanf("%s",str2);
insert(s2,str2);
set<char>::iterator it,del;
set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(dif, dif.begin()));
for (int i=0; str1[i]!='\0'; i++) {
it=dif.find(upper(str1[i]));
del=it;
if (it!=dif.end()) {
printf("%c",(*it));
dif.erase(del);
}
}
printf("\n");
return 0;
}
看了sigmainfy烟客旅人的博客,重新写了一遍:
#include <set>
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
char original[100];
char printed[100];
set <char> results,printedset;
char uppercase(char c){
if (c<='z'&&c>='a'){
c=c-'a'+'A';
}
return c;
}
int main(){
// freopen("/Users/pantingting/Documents/code/data/input", "r", stdin);
scanf("%s",original);
scanf("%s",printed);
for (int i=0; printed[i]!='\0'; i++) {
printedset.insert(uppercase(printed[i]));
}
for (int i=0; original[i]!='\0'; i++) {
char temp=uppercase(original[i]);
if (printedset.count(temp)!=1&&results.count(temp)==0) {
printf("%c",temp);
results.insert(temp);
}
}
printf("\n");
return 0;
}