PAT Advanced 1050 String Subtraction (20) [Hash散列]
题目
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.
题目分析
- 输入s1,s2,打印s=s1-s2(即:打印s1中未出现在s2中的字符)
- s1,s2长度均<=10^4,所有字符都是ASCII码表中的字符,换行符表示输入结束
解题思路
- 定义int asc[256],记录s2中字符出现次数。
- 遍历s1,若asc[s1[i]]>0,说明在s2中出现,不打印
知识点
- 标记全部ASCII码的数组长度设置为256即可
- strlen(),如果直接在for条件中写strlen()容易引起超时
- 使用字符数组接收整行
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散列]的更多相关文章
- PAT Advanced 1050 String Subtraction (20 分)
Given two strings S1 and S2, S=S1−S2 is defined to be the remaining string after taking ...
- 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 ...
- 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 ...
- PAT 甲级 1050 String Subtraction (20 分) (简单送分,getline(cin,s)的使用)
1050 String Subtraction (20 分) Given two strings S1 and S2, S=S1−S2 is defined to be t ...
- PAT练习--1050 String Subtraction (20 分)
题⽬⼤意:给出两个字符串,在第⼀个字符串中删除第⼆个字符串中出现过的所有字符并输出. 这道题的思路:将哈希表里关于字符串s2的所有字符都置为true,再对s1的每个字符进行判断,若Hash[s1[i] ...
- 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 ...
- PAT Advanced 1048 Find Coins (25) [Hash散列]
题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...
- PAT Basic 1047 编程团体赛(20) [Hash散列]
题目 编程团体赛的规则为:每个参赛队由若⼲队员组成:所有队员独⽴⽐赛:参赛队的成绩为所有队员的成绩和:成绩最⾼的队获胜.现给定所有队员的⽐赛成绩,请你编写程序找出冠军队. 输⼊格式: 输⼊第⼀⾏给出⼀ ...
- PAT 解题报告 1050. String Subtraction (20)
1050. String Subtraction (20) Given two strings S1 and S2, S = S1 - S2 is defined to be the remainin ...
随机推荐
- [题解] LuoguP6071 [MdOI2020] Treequery
传送门 感觉这是一个写的很舒服的题? 树上路径的交什么的就很想树上差分?发现根本没法做...它还要求在线.... 好先来看\(Subtask\)吧\(qwq\)... Subtask 1 \(l=r\ ...
- archlinux下安装mysql
mysql的安装 这里安装的是mariadb一个mysql的开源版本,实际使用体验没有差别 1. 安装Maria DB sudo pacman -S mariadb 2. 配置目录 sudo mari ...
- 【剑指Offer】面试题24. 反转链表
题目 定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3 ...
- Python Learning Day9
Scrapy爬虫框架 发送请求 ---> 获取响应数据 ---> 解析数据 ---> 保存数据 Scarpy框架介绍 1.引擎(EGINE) 引擎负责控制系统所有组件之间的数据流,并 ...
- Nginx系列p3:实现一个具有缓存功能的反向代理服务器
今天我们利用 OpenResty 来实现一个反向代理服务器 step1:首先下载安装 OpenResty # 下载安装 OpenResty # 默认安装在 /usr/local 目录下,可在编译时指定 ...
- spring装配bean的三种方式及其混合装配
在spring容器中装配bean有三种基本方式和混合装配方式: 隐式的bean自动发现机制和自动装配 在java中进行显式配置 在xml中配置 混合装配(在多个java文件中配置.在JavaConfi ...
- Python __name__="__main__"的作用
该语句加在模块的最后,可以让这个模块,即可以被别人import,又可以直接运行. fibo.py文件: def fibo(): pass # fibo函数的内容 if __name__==" ...
- python脚本下载 Google Driver 文件
使用python脚本下载 Google Driver 文件 import yaml import sys import requests import os import re import tarf ...
- promise 核心 几个小问题
1.如何改变pending的壮体 抛出异常.pending变为rejected // throw new Error('fail') 内部抛出异常也这样 reason为抛出的error resol ...
- Codeforces 1296E1 - String Coloring (easy version)
题目大意: 给定一段长度为n的字符串s 你需要给每个字符进行涂色,然后相邻的不同色的字符可以进行交换 需要保证涂色后能通过相邻交换把这个字符串按照字典序排序(a~z) 你只有两种颜色可以用来涂 问是否 ...