这类题目也有着常用的套路,写的多了,自然就熟悉了。
题目描述如下:
题目大致意思:
这道题跟1050那道题很像,可以抽象为输入两个字符串s1和s2,输出s1-s2。
大致思路:
如果采用暴力的解题方法,使用两个for循环,仍然会超出时间限制,要使用散列的方法,可以使用一个长度为37的bool型数组,初始为false,对s2遍历,将bool型数组相应的位置置为true,表示该字符未失效。接着对s1进行遍历,如果s1中的字符在s2中未出现过(并且是第一次未出现),则输出该字符。
提交结果:
提交的代码如下:
#include<iostream>
#include<string>
using namespace std;
int hashfunc(char c);
int main()
{
string ans;
bool hashTable[37]={false};
string str1,str2;
cin>>str1>>str2;
for(int i=0;i<str2.size();i++)
{
hashTable[hashfunc(str2[i])]=true;
}
for(int i=0;i<str1.size();i++)
{
if(hashTable[hashfunc(str1[i])]==false)
{
char temp=str1[i];
if(temp>='a'&&temp<='z')
temp=temp-32;
if(ans.find(temp)==string::npos)
{
cout<<temp;
}
ans=ans+temp;
}
}
}
int hashfunc(char c)
{
int result;
if(c>='A'&&c<='Z')
result=c-'A';
else if(c>='a'&&c<='z')
result=c-'a';
else if(c>='0'&&c<='9')
result=c-'0'+26;
else
result=c-'-'+36;
return result;
}
本次提交后累计得分739。