1050 String Subtraction(减法) (20 分)

版权声明:如果喜欢的话,请点击一波关注把,谢谢你,么么哒!转载留名即可 ^_^ https://blog.csdn.net/qq_33375598/article/details/88070446

 

Given two strings S​1​​ and S​2​​, S=S​1​​−S​2​​ is defined to be the remaining string after taking all the characters in S​2​​ from S​1​​. Your task is simply to calculate S​1​​−S​2​​ 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 S​1​​ and S​2​​, respectively. The string lengths of both strings are no more than 10​4​​. 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 S​1​​−S​2​​ in one line.

Sample Input:

They are students.
aeiou

Sample Output:

Thy r stdnts.

参考代码:

#include<cstdio>
#include<cstring>
const int maxn = 10010;
char str1[maxn], str2[maxn];
bool hashtable[128] ={false};//记录字符是否在第二个字符串里出现

int main(int argc, char const *argv[])
{
  fgets(str1, maxn + 1, stdin);
  fgets(str2, maxn + 1, stdin);
  int len1 = strlen(str1) - 1;
  int len2 = strlen(str2) - 1;

for (int i = 0; i < len2; ++i)
{
  hashtable[str2[i]] = true;//第二个字符串里的字符的table位置true
}

for (int i = 0; i < len1; ++i)
{
  if(hashtable[str1[i]] == false) printf("%c", str1[i]);//如果在第二个字符串中俄密友出现过
}

printf("\n");
  return 0;
}

 

没有更多推荐了,返回首页