首页 > 试题广场 >

String Subtraction (20)

[编程题]String Subtraction (20)
  • 热度指数:2170 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
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 .

输入描述:
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.
示例1

输入

They are students.
aeiou

输出

Thy r stdnts.
//https://pintia.cn/problem-sets/994805342720868352/problems/994805429018673152
#include<stdio.h>
#define N 10002 
int main()
{
	int n,i,j,a,b;
	char s1[N],s2[N];
	int fl[130]={0};
	gets(s1);
	gets(s2);
	for(i=0;s2[i]!=NULL;i++){
		fl[s2[i]]=1;
	}
	for(i=0;s1[i]!=NULL;i++)
		if(!fl[s1[i]])
			printf("%c",s1[i]);
	return 0;
}
/*
They are students.
aeiou
*/
C语言比较快
发表于 2019-10-05 21:48:07 回复(0)
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		String s1=in.nextLine();
		String s2=in.nextLine();
		int[] a=new int[256];
		for(int i=0;i<s2.length();i++){
			int t=(int)s2.charAt(i);
			a[t]++;
		}
		for(int i=0;i<s1.length();i++){
			int t=(int)s1.charAt(i);
			if(a[t]==0)
				System.out.print(s1.charAt(i));
		}
	}
}

发表于 2017-01-25 16:12:05 回复(0)
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string s1,s2;
    char c;
    while((c=cin.get())!='\n')
    {
        s1=s1+c;
    }
    while((c=cin.get())!='\n')
    {
        s2=s2+c;
    }
    string::size_type index;
    string::iterator it=s1.begin();
    for(int i=0;i<s2.length();i++)
    {
        while((index=s1.find(s2[i]))!=string::npos)
        {
            s1.erase(index,1);
        }
    }
    cout<<s1<<endl;
    return 0;
}

发表于 2015-06-22 18:53:18 回复(0)
莫名其妙排到第一
#include<iostream>
(720)#include<string>
using namespace std;
int main() {
	string s, t;
	getline(cin, s);
	getline(cin, t);
	int num[129] = { 0 };
	for (int i = 0; i < t.size(); i++) {
		num[t[i]]++;
	}
	for (int i = 0; i < s.size(); i++) {
		if (num[s[i]]) {
			s.erase(i, 1);
			i--;
		}
	}
	cout << s << '\n';
}


发表于 2020-03-26 11:22:12 回复(0)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4+10;
int main(){
    char s[N],ch;
    unordered_map<char,bool> mp;
    int k = 0;
    while((ch=getchar())!='\n') s[k++] = ch;
    while((ch=getchar())!='\n') mp[ch] = true;
    for(int i = 0; i < k; i++)
        if(!mp[s[i]]) printf("%c",s[i]);
    return 0;
}

发表于 2019-04-29 08:10:18 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String line=sc.nextLine();
        String str=sc.nextLine();
        for(int i=0;i<str.length();i++){
            line=line.replace(str.charAt(i)+"","");
        }
        System.out.println(line);
    }
}

发表于 2018-10-05 10:15:32 回复(0)
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1, s2;
    getline(cin, s1);
    getline(cin, s2);
    int i = s1.size();
    int j = 0, k = 0;
    for (j = 0; j < i; j++)
    {
        if (s2.find(s1[j]) == -1)
        cout << s1[j];
    }
    return 0;
}

发表于 2018-08-19 20:55:56 回复(0)
#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{     string s1,s2,s="";     int hash[130] = {0};     getline(cin, s1);     getline(cin, s2);     for(int i=0;i<s2.length();i++)         hash[s2[i]] = 1;     for(int i=0;i<s1.length();i++)     {         if(hash[s1[i]] == 0)             s += s1[i];     }     cout<<s<<endl;     return 0;
}

发表于 2018-03-18 00:33:03 回复(0)
直接暴力了,但是pat会提示超时,由于ascii有限,所以可以用一个hash方法,存储要删除的,输出时候加个判断就可以了,就没有超时了

#include<cstdio>

#include<string.h>

#include<iostream>

#include<string>

using namespace std;

int main()

{

    int isIn[10010];

    memset(isIn,0,sizeof(isIn));

    string s1,s2;

    getline(cin,s1);

    getline(cin,s2);


    for(int j=0;j<s2.size();j++)

    {

        isIn[s2[j]]=1;

    }

    

    for(int i=0;i<s1.size();i++)

    {

        

        if(isIn[s1[i]]==0)

        {

            cout<<s1[i];

        }

    }

    cout<<endl;


    return 0;

}



=========附上超时的方法
#include<cstdio>

#include<iostream>

#include<string>

using namespace std;

int main()

{

string s1,s2;

getline(cin,s1);

getline(cin,s2);


for(int i=0;i<s1.size();i++)

{

bool needPrint = true;

for(int j=0;j<s2.size();j++)

{

if(s1[i]==s2[j])

{

needPrint = false;

break;

}

}

if(needPrint)

{

cout<<s1[i];

}

}

cout<<endl;


return 0;

}

编辑于 2018-03-08 22:26:09 回复(0)
#include <iostream>
#include <cstring>
using namespace std;

int main(){
    int hash[128]={0};
    char a[10010],b[10010];
    gets(a);gets(b);             //字符串有空格,不要用scanf
    int len1=strlen(a);
    int len2=strlen(b);
    for(int i=0;i<len2;i++)
        hash[b[i]]=1;
    for(int i=0;i<len1;i++){
        if(hash[a[i]]==0)
            cout<<a[i];
    }
    return 0;
}

编辑于 2018-01-25 23:43:31 回复(0)
因为ASCLL码个数有限,个人感觉bool数组最快了。。如果有不对请大佬指教
代码如下:
#include <iostream>
using namespace std;

bool notVisual[1000];            //    懒得memset就全局了

int main(int argc, const char* argv[]) {
	ios::sync_with_stdio(false);
	char* str = new char[10005];        //    直接char str[10005]好像提交报错
	char read;
	int len;
	for (len = 0;cin.get(read), read - '\n';len++)str[len] = read;
	str[len] = '\0';
	while (cin.get(read), read - '\n')
		if (!notVisual[read])notVisual[read] = true;
	for (int i = 0;i < len;i++)if (!notVisual[str[i]])cout << str[i];

	str[0] = '\0';
	delete[] str;
	//system("pause");
	return 0;
}

发表于 2017-02-12 15:57:46 回复(0)

python解法:

a,b=input(),input()
for i in b:
    a=a.replace(i,"")
print(a)
发表于 2017-10-11 11:00:09 回复(1)
C++
#include<iostream>
using namespace std;

int main() {
	string s1,s2;
	getline(cin,s1);
	getline(cin,s2);
	for(int i=0;i<s1.size();i++){
		char &ch = s1[i];
		if(s2.find(ch)==string::npos){
			printf("%c",ch);
		}
	}
    return 0;
}


发表于 2020-06-02 21:15:00 回复(0)
#include<bits/stdc++.h>
using namespace std;

bool hashtable[128];

int main(){
	string s1,s2;
	getline(cin,s1);
	getline(cin,s2);
	int n=s1.size();
	int m=s2.size();
	for(int i=0;i<m;i++){
		char c=s2[i];
		hashtable[c]=1;
	}
	for(int i=0;i<n;i++){
		char c=s1[i];
		if(!hashtable[c]){
			cout<<c;
		}
	}
	cout<<endl;
	return 0;
}

发表于 2022-11-11 10:17:07 回复(0)
//String Subtraction (20分)
#include <iostream>
(720)#include <algorithm>

using namespace std;

bool comp(char &a, char &b) {
    return a < b;
}

int main() {
    string cha1, cha2;
    getline(cin, cha1);
    getline(cin, cha2);
    sort(cha2.begin(), cha2.end(), comp);
    for (int i = 0; i < cha1.size(); i++) {
        int j = lower_bound(cha2.begin(), cha2.end(), cha1[i]) - cha2.begin();
        if (cha1[i] != cha2[j]) cout << cha1[i];
    }
}

发表于 2020-03-20 17:42:48 回复(0)
A=input()
S=set(input())
res=''
for e in A:
    if e not in S:
        res+=e
print(res)

发表于 2018-09-04 09:45:08 回复(0)
//3ms
#include <cstdio>
bool hashTable[128] = {false};
char s[10010];
int main(){
    fgets(s, 10010, stdin);
    char c;
    while((c=getchar()) != '\n'){
        hashTable[c] = true;
    }
    for(int i = 0; s[i] != '\n'; ++i){
        if(hashTable[s[i]] == false)printf("%c", s[i]);
    }
    return 0;
}
发表于 2018-07-28 10:15:50 回复(0)
#include<stdio.h>
#include<iostream>
#include<string>
#include<set>
using namespace std;

int main() {

    string str1,str2;
    getline(cin,str1);
    getline(cin,str2);
    //cin>>str1>>str2;
    set<char> dic;
    for(int i=0;i<str2.size();i++){

        dic.insert(str2[i]);
    }
    for(int i = 0;i<str1.size();i++){
        if(dic.find(str1[i])==dic.end()){
            cout<<str1[i];
        }
    }
    getchar();

    return 0;
}
发表于 2018-07-19 14:22:05 回复(0)
s1 = input()
s2 = set(input())
ans = ''
for x in s1:
    if x not in s2:
        ans += x
print(ans)

发表于 2018-04-19 21:24:49 回复(0)
#include<iostream>
#include<string>
using namespace std;

int main(){
    int keys[255];
    for(int i=0;i<255;i++) keys[i] = 1;
    string s1,s2;
    getline(cin,s1);
    getline(cin,s2);
    for(int i=0;i<s2.length();i++){
        keys[s2[i]] = 0;
    }
    for(int i=0;i<s1.length();i++){  if(!keys[s1[i]]) continue;  cout<<s1[i];  }  cout<<endl;
    return 0;
}

编辑于 2018-03-12 21:13:44 回复(0)

问题信息

难度:
25条回答 6648浏览

热门推荐

通过挑战的用户