题目
Given two strings S1 and S2, S = S1 - S2 is defined to be the remaining string after taking all the characters in S2 from S1. Your task is simply to calculate S1 - S2 for any given strings. However, it might not be that simple to do it fast.
Input Specification:
Each input file contains one test case. Each case consists of two lines which gives S1 and S2, respectively. The string lengths of both strings are no more than 104. It is guaranteed that all the characters are visible ASCII codes and white space, and a new line character signals the end of a string.
Output Specification:
For each test case, print S1 - S2 in one line.
Sample Input:
They are students. aeiou
Sample Output:
Thy r stdnts.
char的范围有限,统一标记一下s2中出现的元素,去除即可。
代码:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
char s1[10002],flag[255]={0}; //s1,标志(0代表相应字符在s2中没出现,1代表出现)
int i=0;
while((s1[i]=getchar())!='\n') //输入s1,注意getchar用法
i++;
char c;
while((c=getchar())!='\n') //处理s2
flag[c]=1;
int j;
for(j=0;j<i;j++) //输出字符串
if(flag[s1[j]]==0)
printf("%c",s1[j]);
return 0;
}