题目

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. 057-while循环

    <?php $x=1; //初始化变量 while($x<=5){ //执行while循环 echo "$x<br />"; $x++; } ?>

  2. 二、react开发环境配置与webpack入门

    Webpack 模块打包工具(module bundler)功能: 将 CSS.图片与其他资源打包 打包之前预处理(Less.CoffeeScript.JSX.ES6 等)档案 依 entry 文件不 ...

  3. MacOS通过ssh连接基于Virtualbox的Ubuntu虚拟机

    以前总是用Windows软件putty进行ssh连接,今天尝试使用macos. 实验环境:主机:macos 10.15.3 客户机:Ubuntu 18.04 默认情况下,Ubuntu没有安装SSH,需 ...

  4. 中国移动携手华为百度展示5G应用,实现8K视频传输

    在今日举行的 2019 年百度云智峰会上,中国移动携手华为和百度,首次展示基于 SA 架构的 5G Vertical LAN (行业局域网)技术,承载 8K 实时会议系统,助力企业云办公.该技术可为合 ...

  5. mysql日期

    查询当前时间 select now() 结果:2017-04-24 18:11:26 格式化当前日期 SELECT DATE_FORMAT(NOW(), '%Y-%m-%d') 结果:2017-04- ...

  6. UVA_11525 树状数组的活用 二分

    我们知道1——k有K!种排列,现在给定k和n,要你按字典序输出 第n种排列的数列 而且题目给的 n是 n=S1(k-1)!+S2(k-2)!+...+Sk-1*1!+Sk*0!(0=<Si< ...

  7. 查看 vps 进程网络流量

    弄好了 vps 以后,感觉网络流量走的有点多,决定查查看到底什么情况. 首先安装 sar 来看看各个设备消耗的流量 apt-get install sysstat sar 的参数 DEV 表示网口, ...

  8. MySQL笔记 01

    STRUCTURE QUERY LANGUAGE 数据库CRUD操作 DDL: 数据库定义语言,定义数据库数据表结构 CREATE(创建): 创建数据库 CREATE DATABASE 数据库名字; ...

  9. (BUILDER)建造者与(FACTORY)工厂模式 的比较

    首先,说明下 参考博文 1.   建造者      http://www.cnblogs.com/zhili/p/BuilderPattern.html 2.   抽象工厂    http://www ...

  10. [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war

    创建springboot项目,且不采用<parent>引入springboot时,pom.xml如下: <?xml version="1.0" encoding= ...