问题 D: String Subtraction (20)
时间限制: 1 Sec 内存限制: 32 MB
提交: 516 解决: 243
[提交][状态][讨论版][命题人:外部导入]
题目描述
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 - S2for any given strings. However, it might not be that simple to do it fast.
输入
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.
输出
For each test case, print S1 - S2 in one line.
样例输入
They are students.
aeiou
样例输出
Thy r stdnts.
需要注意的是
1. 只有一个输入案例
2.最后一行不能换行,不然输出超限
#include<cstdio>
#include<cstring>
const int maxn = 10010;
int main(){
char s1[maxn],s2[maxn];
gets(s1);
gets(s2);
int hashTable[127]={0}; //ASCII码对应的整型数只有0-127
for(int i=0;i<strlen(s2);i++){
hashTable[s2[i]]++;
}
for(int i=0;i<strlen(s1);i++){
if(hashTable[s1[i]]==0)
printf("%c",s1[i]);
}
return 0;
}
本文介绍了一种快速计算两个字符串S1和S2的差集S的算法,即从S1中移除所有出现在S2中的字符。通过使用哈希表来记录S2中每个字符的出现次数,然后遍历S1,输出未在S2中出现的字符,实现高效字符串处理。
300

被折叠的 条评论
为什么被折叠?



