【PAT甲级真题】- String Subtraction (20)

题目来源

String Subtraction - PTA
String Subtraction - 牛客

Description

Given two strings S 1 S_1 S1and S 2 , S = S 1 − S 2 S_2,S=S_1−S_2 S2,S=S1−S2is defined to be the remaining string after taking all the characters in S 2 S_2 S2from S 1 S_1 S1. Your task is simply to calculate S 1 − S 2 S_1−S_2 S1−S2for 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 S_1 S1and S 2 S_2 S2, respectively. The string lengths of both strings are no more than 10 4 10^4 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 S 1 − S 2 S_1−S_2 S1−S2in one line.

Sample Input:

复制代码
They are students.
aeiou

Sample Output:

复制代码
Thy r stdnts.

题目大意

给定两个字符串 S 1 , S 2 S_1,S_2 S1,S2 ,定义 S 1 − S 2 S_1-S_2 S1−S2 是字符串 S 1 S_1 S1 中去掉所有出现在 S 2 S_2 S2 的字符得到的字符串,要求输出这个字符串

思路简介

因为字符用 ASCII 码表示,共 128 个,可以用一个桶记录某个字符是否在 S 2 S_2 S2 出现,如果出现就不输出这个字符

遇到的问题

代码

cpp 复制代码
/**
 * String Subtraction
 * https://pintia.cn/problem-sets/994805342720868352/exam/problems/type/7?problemSetProblemId=994805429018673152
 * https://www.nowcoder.com/pat/5/problem/4089
 * 桶排
 */
#include<bits/stdc++.h>
using namespace std;

int s[300];

void solve(){
    string s1,s2;
    getline(cin,s1);
    getline(cin,s2);
    int len1=s1.size(),len2=s2.size();
    for(int i=0;i<len2;++i){
        s[s2[i]-'\0']=1;
    }
    for(int i=0;i<len1;++i){
        if(!s[s1[i]-'\0'])cout<<s1[i];
    }
    cout<<'\n';
}

int main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //fstream in("in.txt",ios::in);cin.rdbuf(in.rdbuf());
    int T=1;
    //cin>>T;
    while(T--){
        solve();
    }
    return 0;
}
相关推荐
凡人叶枫5 小时前
Effective C++ 条款30:透彻了解 inlining 的里里外外
linux·开发语言·c++·嵌入式开发·effective c++
noipp5 小时前
推荐题目:洛谷 P10907 [蓝桥杯 2024 国 B] 蚂蚁开会
c语言·c++·算法·编程·洛谷
学逆向的5 小时前
C++纯虚函数
开发语言·c++·网络安全
程序员二叉6 小时前
【JUC】线程池全套深度详解|参数|流程|拒绝策略|调优|异常处理
java·开发语言·jvm·算法·面试·juc
青山木6 小时前
Hot 100 --- 轮转数组
java·数据结构·算法
徐小夕6 小时前
Loop Engineering 深度解析与实战指南(全网最全)
前端·算法·github
凡人叶枫6 小时前
Effective C++ 条款22:将成员变量声明为 private
linux·开发语言·c++
北域码匠7 小时前
SHA-1算法:安全哈希原理与应用解析
算法·c#·哈希算法
坚果派·白晓明8 小时前
【鸿蒙PC】SDL3 移植:AtomCode Skills 4 步速通多媒体库适配
c++·华为·ai编程·harmonyos·atomcode·c/c++三方库
手写码匠8 小时前
手写 GraphRAG:从零实现图增强检索增强生成系统
人工智能·深度学习·算法·aigc