先将两个字符串排序,然后对应相同字符标记删除
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
bool flag[10001];
struct str{
char ch;
int index;
} str1[10001],str2[10001];
bool cmp_str(const struct str &a, const struct str&b){
return a.ch < b.ch;
}
bool cmp_index(const struct str &a, const struct str &b){
return a.index < b.index;
}
int main(){
int i = 0;
freopen("1.in", "r", stdin);
while ((str1[i].ch = getchar()) != '\0'&&str1[i].ch != '\n'){
str1[i].index = i;
i++;
}
int len1 = i, len2;
i = 0;
while ((str2[i].ch = getchar()) != '\0'&&str2[i].ch != '\n'){
str2[i].index = i;
i++;
}
len2 = i;
sort(str1, str1 + len1, cmp_str);
sort(str2, str2 + len2, cmp_str);
int j;
i = j = 0;
while (i < len1&&j < len2){
if (str1[i].ch < str2[j].ch)
i++;
else if (str1[i].ch > str2[j].ch)
j++;
else if (str1[i].ch == str2[j].ch)
{
flag[str1[i].index] = true;
i++;
}
}
sort(str1, str1 + len1, cmp_index);
for (i = 0; i < len1; i++)
if (!flag[i])
putchar(str1[i].ch);
putchar('\n');
return 0;
}