题目

Given two strings S1 and S2, S = S1 – S2 is defined to be the remaining string afer 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.

Input Specification:

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.

Output Specification:

For each test case, print S1 – S2 in one line.

Sample Input:

They are students.

aeiou

Sample Output:

Thy r stdnts.

题目分析

  1. 输入s1,s2,打印s=s1-s2(即:打印s1中未出现在s2中的字符)
  2. s1,s2长度均<=10^4,所有字符都是ASCII码表中的字符,换行符表示输入结束

解题思路

  1. 定义int asc[256],记录s2中字符出现次数。
  2. 遍历s1,若asc[s1[i]]>0,说明在s2中出现,不打印

知识点

  1. 标记全部ASCII码的数组长度设置为256即可
  2. strlen(),如果直接在for条件中写strlen()容易引起超时
  3. 使用字符数组接收整行
char ca[10001];
cin.getline(ca,10001);//不可以修改为strlen(ca),因为strlen是获取已填充字符长度

Code

Code 01(string)

#include <iostream>
using namespace std;
int main(int argc, char * argv[]) {
string s1,s2;
getline(cin,s1);
getline(cin,s2);
int asc[256]= {0};
for(int i=0; i<s2.length(); i++) {
asc[s2[i]]++;
}
for(int i=0; i<s1.length(); i++) {
if(asc[s1[i]]>0)continue;
printf("%c",s1[i]);
}
return 0;
}

Code 02(char array)

#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char * argv[]) {
char s1[10001],s2[10001];
cin.getline(s1,10001);
cin.getline(s2,10001);
int asc[256]= {0};
int len1=strlen(s1),len2=strlen(s2);//如果直接在for条件中写strlen()容易引起超时
for(int i=0; i<len2; i++) asc[s2[i]]++;
for(int i=0; i<len1; i++) {
if(asc[s1[i]]>0)continue;
printf("%c",s1[i]);
}
return 0;
}

PAT Advanced 1050 String Subtraction (20) [Hash散列]的更多相关文章

  1. PAT Advanced 1050 String Subtraction (20 分)

    Given two strings S​1​​ and S​2​​, S=S​1​​−S​2​​ is defined to be the remaining string after taking ...

  2. PAT Advanced 1084 Broken Keyboard (20) [Hash散列]

    题目 On a broken keyboard, some of the keys are worn out. So when you type some sentences, the charact ...

  3. PAT Advanced 1041 Be Unique (20) [Hash散列]

    题目 Being unique is so important to people on Mars that even their lottery is designed in a unique wa ...

  4. PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)

    1050 String Subtraction (20 分)   Given two strings S​1​​ and S​2​​, S=S​1​​−S​2​​ is defined to be t ...

  5. PAT练习--1050 String Subtraction (20 分)

    题⽬⼤意:给出两个字符串,在第⼀个字符串中删除第⼆个字符串中出现过的所有字符并输出. 这道题的思路:将哈希表里关于字符串s2的所有字符都置为true,再对s1的每个字符进行判断,若Hash[s1[i] ...

  6. PAT Advanced 1134 Vertex Cover (25) [hash散列]

    题目 A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at ...

  7. PAT Advanced 1048 Find Coins (25) [Hash散列]

    题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...

  8. PAT Basic 1047 编程团体赛(20) [Hash散列]

    题目 编程团体赛的规则为:每个参赛队由若⼲队员组成:所有队员独⽴⽐赛:参赛队的成绩为所有队员的成绩和:成绩最⾼的队获胜.现给定所有队员的⽐赛成绩,请你编写程序找出冠军队. 输⼊格式: 输⼊第⼀⾏给出⼀ ...

  9. PAT 解题报告 1050. String Subtraction (20)

    1050. String Subtraction (20) Given two strings S1 and S2, S = S1 - S2 is defined to be the remainin ...

随机推荐

  1. 八十六、SAP中ALV的事件查看

    一.事务代码SE37,点击运行 二.再点击执行 三.我们可以看到有17个事件,点击17前面的表格图标 四.来到详细的事件中 五.我们回到SE37,点击显示 六.查看参数,为一个内表 七.我们点击SLI ...

  2. msf中arp_sweep使用报错:usbmon1:ERROR while getting interface flags:no such device

    在许多的工具使用中,会出现很多的错误,要养成先思考再去寻找帮助的习惯 在用use命令使用arp_sweep模块的时候爆出错误:usbmon1:ERROR while getting interface ...

  3. Spring 框架介绍

    Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Control – I ...

  4. (四)requests模块的cookies和代理操作

    基于requests模块的cookie操作 引言:有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取某个人“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到 ...

  5. Http协议Get与Post请求

    摘要:https://blog.csdn.net/kebi007/article/details/103059900 不就是get拼接url,post传body,get限制字符串长度吗! 请求缓存:G ...

  6. 基础语法-其它流程控制语句break和continue

    基础语法-其它流程控制语句break和continue 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.break语句 /** * break语句 * @author 尹正杰 * ...

  7. TX2-刷机完成后安装程序ubuntu_linux命令&TX2学习总结

    Linux教程|菜鸟教程:http://www.runoob.com/linux/linux-tutorial.html 认识linux:ping命令:ping命令是常用的网络命令ping网关:pin ...

  8. 18 ~ express ~ 前台分类导航展示 与 排序

    一,前台分类导航展示 1,后台文件:  /router/main.js router.get('/',(req,res,next)=>{ /** * 从数据库中读取分类信息 * rs是一个数组类 ...

  9. Day3-T4

    原题目 Describe:有点恶心的DP+最短路 code: #include<bits/stdc++.h> using namespace std; long long A,B,C,z, ...

  10. Django学习路线