【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;
}
相关推荐
KaMeidebaby1 天前
卡梅德生物技术快报|PD1 单克隆抗体定制配套 N 糖全谱质控开发
前端·人工智能·算法·数据挖掘·数据分析
8Qi81 天前
LeetCode 235. 二叉搜索树的最近公共祖先(LCA)
算法·leetcode·二叉树·递归·二叉搜索树·lca·迭代
好评1241 天前
【C++】智能指针全解
c++·智能指针
bIo7lyA8v1 天前
算法稳定性分析中的随机扰动建模的技术8
算法
是阿建吖!1 天前
【Linux】信号
android·linux·c语言·c++
城北徐宫1 天前
Linux信号深度解剖:5种产生、3张表、4次切换
linux·c++·学习
sugar__salt1 天前
从栈队列数据结构到JS原型面向对象全解
前端·javascript·数据结构
liulilittle1 天前
论 Linux 内核态全局稳态带宽的卡尔曼估计与工程实现
linux·服务器·网络·c++·计算机网络·tcp·通信
XBodhi.1 天前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio
科研online1 天前
基于多源数据和XGBoost-SHAP分析中国大陆绿地碳汇空间变异影响因素的非线性相关性与尺度差异
算法·学习方法