1051 Pop Sequence (25分)  [stack]

简答的栈模拟题,只要把过程想清楚就能做出来。

扫描到某个元素时候,假如比栈顶元素还大,说明包括其本身的在内的数字都应该入栈。将栈顶元素和序列比对即可,相同则弹栈,继续扫描;否则无法生成满足条件的序列。注意栈满时不能入栈

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e3+100;
int m, n, q, a[maxn], sta[maxn];
bool flag;
int main(){
scanf("%d%d%d", &m, &n, &q);
while(q--){
for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
int k = 1, max_num = 0, t = 0;
flag = true;
while(k<=n){
while(max_num<a[k]&&t<m) sta[++t] = ++max_num;
if(sta[t]==a[k]) t--, k++;
else{
flag = false;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
}

1056 Mice and Rice (25分)  [queue]

这题意我看了很久都没明白,后来看到别的博客才知道是什么意思,才开始动手写

题目的要求是让你模拟一场比赛,n个参赛选手,k人一组,最后的不足k人也视为一组。然后每轮比赛决出一个冠军,进入下一组,直到最后只剩下一个人。所有在同一轮比赛中输掉的人排名相同。

我的思路是将参赛者排入优先队列,得出每场比赛优胜者继续分入优先队列,重复上述过程得出冠军,中途记录每个人所处的轮次。之后可以根据每个人所处的轮次得到他的排名。能解决上述的两个小点这题就搞定了,但是我还是卡了很久,调试的时候有点粗心。

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
using namespace std;
const int maxn = 1e3+100;
priority_queue<pii, vector<pii>, less<pii> >que[maxn];
int n, m, a[maxn];
int id, rnd[maxn], cnt[maxn];
int main(){
scanf("%d%d", &n, &m);
for(int i = 0; i <= n-1; i++) scanf("%d", &a[i]);
for(int i = 0; i <= n-1; i++){
scanf("%d", &id);
que[i/m].push({a[id], id}), rnd[id]++;
}
int l = 0, r = (n-1)/m;
while(l<r){
//cout << l << " " << r << endl;
for(int i = l; i <= r; i++){
pii P = que[i].top();
que[(i-l)/m + r+1].push(P), rnd[P.second]++;
// cout << P.second << " ";
}
// cout << endl;
int tmp = (r-l)/m;
l = r + 1, r = tmp + l;
}
int maxRound = ++rnd[que[r].top().second];
for(int i = 0; i <= n-1; i++) cnt[rnd[i]]++;
for(int i = maxRound-1; i >= 1; i--) cnt[i] += cnt[i+1];
for(int i = 0; i <= n-1; i++) printf("%d ", cnt[rnd[i]+1]+1);
}

reference:

https://blog.csdn.net/CV_Jason/article/details/85238006

https://www.nowcoder.com/questionTerminal/667e3c519c30492fbb007fbb42f44bff

1060 Are They Equal (25分)

这题有点把我恶心到了,得分确实不是很难,但是要把分拿全还是有很多地方需要去考虑的。按照算法笔记的样例测试下自己的代码,后来改了很久才过了,这本书确实还挺那不错的,给了很多指导。之后有空的话,可以根据书中的知识点稍作总结。

题目大意是说将两个数改写成科学计数法的形式,然后判断他们是否相等。根据科学计数法的定义,只需要判断本体部分和指数是否相等即可。这样一来问题就转化成如何求给定浮点数的本体部分和指数。本体部分自然是要去掉前导0,这是关键。指数部分的话需要分三种情况,一种是0.0001345这种,指数为负数;一种是1234.678,指数为正数,一种是0.0000,指数为0;需要细心对这几种情况进行处理。

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
using namespace std;
const int maxn = 1e3+100;
int n, num1, num2;
string s1, s2;
int main(){
cin >> n >> s1 >> s2;
int len1 = s1.length(), len2 = s2.length(), p1 = s1.find('.'), p2 = s2.find('.');
if(p1==-1) num1 = s1.length();
else num1 = p1, s1.erase(p1, 1);
if(p2==-1) num2 = s2.length();
else num2 = p2, s2.erase(p2, 1);
int tmp1 = n - num1, tmp2 = n - num2;
while(tmp1>0) s1.insert(s1.end(), '0'), tmp1--;
while(tmp2>0) s2.insert(s2.end(), '0'), tmp2--;
//cout << s1 << " " << s2;
if(s1.substr(0, n)==s2.substr(0, n))
cout << "YES 0." << s1.substr(0, n) << "*10^" << num1 << endl;
else
cout << "NO 0." << s1.substr(0, n) << "*10^" << num1 << " 0." << s2.substr(0, n) << "*10^" << num2; }

WA

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
using namespace std;
int n, e1, e2;
string s1, s2, s3, s4;
string deal(string s, int &e){
//cout << s << " ";
while(s[0]=='0') s.erase(s.begin());
if(s[0]=='.'){
s.erase(s.begin());
while(s[0]=='0') s.erase(s.begin()), e--;
if(s.length()==0) e = 0;
}
else {
e = s.find('.');
if(e==-1) e = s.length();
else s.erase(e, 1);
}
int tmp = n - e;
while(tmp>0) s.insert(s.end(), '0'), tmp--;
//cout << s << endl;
return s.substr(0, n);
}
int main(){
cin >> n >> s1 >> s2;
s3 = deal(s1, e1);
s4 = deal(s2, e2);
//cout << s3 << " " << s4;
if(s3==s4&&e1==e2)
cout << "YES 0." << s3 << "*10^" << e1 << endl;
else
cout << "NO 0." << s3 << "*10^" << e1 << " 0." << s4 << "*10^" << e2; }

AC

1039 Course List for Student (25分)

map和vector的基本使用,最后记得对vector进行排序就可以了

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 3e3+100;
map<string, vector<int> > mp;
string s;
int n, m, id, q;
int main(){
scanf("%d%d", &n, &m);
while(m--){
scanf("%d%d", &id, &q);
while(q--){
cin >> s;
mp[s].pb(id);
}
}
while(n--){
cin >> s;
int len = mp[s].size();
cout << s << " " << len;
sort(mp[s].begin(), mp[s].end());
for(int i = 0; i < len; i++)
cout << " " << mp[s][i];
cout << endl;
}
}

1047. Student List for Course
vector的基本用法,但是最后一个测试点我超时了,原因是大量使用cout。这样一来解决方案有两种,一种是开始存储的时候就使用char*,另外一种为了方便起见,存储使用string,最后用string.to_str()转化使用printf输出,不过要注意to_str()的用法

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 3e3+100;
vector<string> g[maxn];
string s;
int n, m, id, q;
int main(){
scanf("%d%d", &n, &m);
while(n--){
cin >> s >> q;
while(q--){
cin >> id;
g[id].pb(s);
}
}
for(int i = 1; i <= m; i++){
cout << i << " " << g[i].size() << endl;
sort(g[i].begin(), g[i].end());
int len = g[i].size();
for(int j = 0; j < len; j++)
printf("%s\n", g[i][j].c_str());
} }

reference:

https://www.liuchuo.net/archives/2147

https://www.xingmal.com/article/article/1234485790168453120

https://blog.csdn.net/wangshubo1989/article/details/49872717

https://www.cnblogs.com/newzol/p/8686076.html

1071 Speech Patterns (25分)

理解错题目意思了,题干还带干扰,导致后来WA后都不知道什么原因,发现自己连题目对单词的定义都没看清楚,当时觉得这句话太长了,就跳过了

题目意思很简单,统计一句话中出现次数最多的单词的个数。这里的单词定义为被非数字、字母分割的连续数字、字母序列(e.g. aaa***aaaa需要进行分割):

Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

剩下的就是直接统计了,不过需要注意读取字符的方式,这里可以使用getline(cin, str) 读入一整行

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 1e8+100;
string str, id;
int t, res;
char ch;
map<string, int> mp;
bool check(char ch){
if(ch>='a'&&ch<='z') return true;
else if(ch>='A'&&ch<='Z') return true;
else if(ch>='0'&&ch<='9') return true;
else return false;
}
int main(){
getline(cin, str);
while(str[t]){
string word;
while(check(ch=str[t++])){
if((ch>='a'&&ch<='z')||(ch>='0'&&ch<='9')) word += ch;
else if(ch>='A'&&ch<='Z') word += ch-'A'+'a';
else if(word!="") word.clear();
}
if(word!="") {
// cout << word << " ";
if(++mp[word]>=res) id = word , res = mp[id];
else if(mp[word]==res) id = id=="" ? word : min(id, word);
}
}
cout << id << " " << res;
}

Reference:

https://blog.csdn.net/a3192048/article/details/80303547

https://blog.csdn.net/qq_41325698/article/details/103424661

1100 Mars Numbers (20分)

map的基本使用,不难但是这种字符串的题目处理起来有点小麻烦。

还有两点需要注意:

  • tret字符串长度为4啊,不要默认它长度为3
  • 火星文进位没有0,比如26 输入的是 hel 而不是hel tret(题目中并没有指明)

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 1e8+100;
int t;
string str;
map<string, int> num;
string low[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
string high[13] = {"tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok","mer", "jou" };
int main(){
for(int i = 0; i < 13; i++) num[low[i]] = i, num[high[i]] = 13*i;
scanf("%d", &t);
getchar();
while(t--){
getline(cin, str);
int len = str.length();
if(str[0]>='0'&&str[0]<='9') {
int sum = 0;
for(int i = 0; i < len; i++) sum += (str[i]-'0')*pow(10, len-1-i);
if(sum<13) cout << low[sum] << endl;
else {
cout << high[sum/13];
if(sum%13!=0) cout << " " << low[sum%13];
cout << endl;
}
}
else if(len<=4) cout << num[str] << endl;
else cout << num[str.substr(0, 3)]+num[str.substr(4, 3)] << endl;
}
}

Reference:

https://blog.csdn.net/qq_26398495/article/details/79159381

1022 Digital Library (30分)

map的基本使用,需要注意两点:

  • getline的使用,详细见Refernce
  • 记录id的时候最好使用string而不是int,可能会碰到0000111的格式,或者输出时限制格式也可

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 1e8+100;
map<string, vector<string> >mp;
string id, title, author, keyword, publisher, date, query;
int t, q;
int main(){
scanf("%d", &t);
while(t--){
cin >> id, getchar();
getline(cin, title), getline(cin, author);
// cout << id << " " << title << " " << author << endl;
mp[title].pb(id), mp[author].pb(id);
while(cin>>keyword){
mp[keyword].pb(id);
if(getchar()=='\n') break;
}
getline(cin, publisher);
cin >> date;
mp[publisher].pb(id), mp[date].pb(id);
}
cin >> q, getchar();
while(q--){
getline(cin, query);
cout << query << endl;
string tmp = query.erase(0, 3);
int len = mp[tmp].size();
if(len!=0){
sort(mp[tmp].begin(), mp[tmp].end());
for(int i = 0; i < len; i++)
cout << mp[tmp][i] << endl;
}
else cout << "Not Found" << endl;
// cout << query << endl;
}
}

Reference:

https://www.cnblogs.com/zzzlight/p/12541729.html

1063 Set Similarity (25分)

这题就是set的基本使用,最开始做的时候由于把查询的两个数据集结果用set再保存一遍,发现测试点4会超时。

为了降低复杂度,策略改成找两个数据集相同的部分。可以分别使用两个指针指向数据集去判断,不过使用find足以过题,就没有再优化了

#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <set>
#define ll long long
#define inf 0x3f3f3f
#define pii pair<int, int>
#define pb push_back
using namespace std;
const int maxn = 1e8+100;
set<int> s[100];
int n, m, num, q;
int main(){
scanf("%d", &n);
for(int i = 1; i <= n; i++){
scanf("%d", &m);
while(m--){
scanf("%d", &num);
s[i].insert(num);
}
}
scanf("%d", &q);
while(q--){
int a, b, tot = 0;
scanf("%d%d", &a, &b);
int lena = s[a].size(), lenb = s[b].size();
set<int>::iterator it;
for(it = s[a].begin(); it != s[a].end(); it++) {
if(s[b].find(*it)!=s[b].end()) tot++;
}
// cout << lena << " " << lenb << " " << s[n+1].size() << endl;
printf("%.1f%\n", 100.0*tot/(lena+lenb-tot));
}
}

PTA甲级—STL使用的更多相关文章

  1. PTA甲级1094 The Largest Generation (25分)

    PTA甲级1094 The Largest Generation (25分) A family hierarchy is usually presented by a pedigree tree wh ...

  2. PTA甲级B1061 Dating

    目录 B1061 Dating (20分) 题目原文 Input Specification: Output Specification: Sample Input: Sample Output: 生 ...

  3. PTA 甲级 1139

    https://pintia.cn/problem-sets/994805342720868352/problems/994805344776077312 其实这道题目不难,但是有很多坑点! 首先数据 ...

  4. PTA甲级—链表

    1032 Sharing (25分) 回顾了下链表的基本使用,这题就是判断两个链表是否有交叉点. 我最开始的做法就是用cnt[]记录每个节点的入度,发现入度为2的节点即为答案.后来发现这里忽略了两个链 ...

  5. PTA甲级—数学

    1.简单数学 1008 Elevator (20分) 模拟题 #include <cstdio> #include <cstring> #include <string& ...

  6. PTA甲级—常用技巧与算法

    散列 1078 Hashing (25 分) Quadratic probing (with positive increments only) is used to solve the collis ...

  7. PAT 甲级 1041. Be Unique (20) 【STL】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1041 思路 可以用 map 标记 每个数字的出现次数 然后最后再 遍历一遍 找到那个 第一个 ...

  8. PAT 甲级 1128. N Queens Puzzle (20) 【STL】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1128 思路 可以 对每一个皇后 都判断一下 它的 行,列 ,左右对角线上 有没有皇后 深搜解决 ...

  9. PAT甲级——1112 Stucked Keyboard (字符串+stl)

    此文章同步发布在我的CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90041078   1112 Stucked Keyboa ...

随机推荐

  1. Azure Terraform(三)部署 Web 应用程序

    一,引言 上一节关于 Terraform 的文章讲到 Terraform 使用到的一些语法,以及通过演示使用 Terraform 在Azure 上部署资源组,极大的方便了基础设施实施人员,也提高了基础 ...

  2. 腾讯IOT之树莓派物联网设备

    目录 腾讯IOT之树莓派物联网设备 硬件配置 软件配置 Tecent IOT 开发平台的使用 新建项目 新建产品 添加自定义功能 设备开发 微信小程序配置 面板配置 新建设备 使用设备 在线调试 设备 ...

  3. Java的nanoTime()方法

    java有两个获取和时间相关的秒数方法,一个是广泛使用的 System.currentTimeMillis() 返回的是从一个长整型结果,表示毫秒. 另一个是 System.nanoTime() 返回 ...

  4. 每日CSS_滚动页面动画效果

    每日CSS_滚动页面动画效果 2021_1_13 源码链接 1. 代码解析 1.1 html 代码片段 <section> <h2>开 始 滑 动</h2> < ...

  5. OOP、封装、继承、多态,真的懂了吗?

    平时只要一提起来面向对象编程OOP的好处,随口就能说出来,不就是封装.继承.多态么,可他们的含义是什么呢,怎么体现,又有什么非用不可的好处啊.可能平时工作中天天在用OOP,仅仅是在用OOP语言,就是一 ...

  6. SpringCloud Alibaba Nacos服务注册与配置管理

    Nacos SpringCloud Alibaba Nacos是一个狗抑郁构建云原生应用的动态服务发现.配置管理和服务管理平台. Nacos:Dynamic Naming and Configurat ...

  7. ArrayList源码解析--值得深读

    ArrayList源码解析 基于jdk1.8 ArrayList的定义 类注释 允许put null值,会自动扩容: size isEmpty.get.set.add等方法时间复杂度是O(1): 是非 ...

  8. NodeJS之npm、cnpm、npx、yarn

    一.npm 1,概念 npm 是 Node.js 官方提供的包管理工具,他已经成了 Node.js 包的标准发布平台,用于 Node.js 包的发布.传播.依赖控制.npm 提供了命令行工具,使你可以 ...

  9. 通过SE14重建数据库表

    通过程序中的SQL语句向数据库表中插入的内容,系统无法转换,并且已经存在于数据库表中,那么当对该表进行保存数据的修改时,可能会导致该表从数据库中的删除. 举了例子:(完全是为了方便理解) SAP系统, ...

  10. 手机QQ空间自动点赞登录

    学以致用~使用 Appium 模拟人类操控手机行为 V2.0在手机上运行,目前实现以下功能: 1.小黑屋模式,一分钟内给好友发100条消息然后进了好友的小黑屋 2.定时发消息提醒对象多喝热水~ 3.对 ...