1050. String Subtraction (20)
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. aeiouSample Output:
Thy r stdnts.
题意:给出两个字符串,要求在第一个字符串中把第二个字符串中出现所有的字符去掉。
思路:因为卡时比较严格,字符串又比较长,所以要想尽一切方法减少运算,暴力处理肯定超时,所以选用哈希比较好。我使用哈希之后发现还是超时,各种找bug,结果发现在循环处理字符的时候没有把字符串长度保存下来,而是在循环里一直使用strlen函数,导致运算量较大。。血的教训啊!!
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int bin[1000];
char str1[10005],str2[10005];
int main()
{
gets(str1);
gets(str2);
int i,j,k;
int len1=strlen(str1);
int len2=strlen(str2);
for(i=0;i<len2;i++)//这里len2换成strlen(str2),下面的len1换成strlen(len1)就超时了。
{
bin[str2[i]]=1;
}
for(i=0;i<len1;i++)
{
if(bin[str1[i]]==0)
{
printf("%c",str1[i]);
}
}
cout<<endl;
return 0;
}