PAT 2014 秋
A 1084 Broken Keyboard
注意大小写即可。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string> using namespace std; int main()
{
string inStr, outStr;
cin >> inStr >> outStr;
int inIndex = , outIndex = ;
bool charFlag[]={false};
while(inIndex < inStr.size())
{
if(inStr[inIndex] == outStr[outIndex])
{
inIndex++;
outIndex < outStr.size()- ? outIndex++ : outStr[outIndex] = ' ';
}
else
{
int tmpNum = (int)inStr[inIndex++];
tmpNum = tmpNum >= 'a' ? tmpNum-'a'+'A' : tmpNum;
if(!charFlag[tmpNum])
{
charFlag[tmpNum] = ;
printf("%c", tmpNum);
}
}
}
return ;
}
A 1085 Perfect Sequence
1.二分查找,不然会超时
2.注意数值的范围,有些地方需要使用long long
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector> using namespace std;
typedef long long LL;
vector<int> inputNumVec;
int N, P;
int getMaxCnt(int lIndex)
{
int rIndex = inputNumVec.size()-, midIndex;
LL tmpNum = (LL)inputNumVec[lIndex]*P;
while(lIndex <= rIndex)
{
midIndex = (lIndex + rIndex) >> ;
if(inputNumVec[midIndex] > tmpNum)
rIndex = midIndex - ;
else
lIndex = midIndex + ;
}
return rIndex;
}
int main()
{
cin >> N >> P;
inputNumVec.resize(N);
for(int i = ; i < N; ++ i)
cin >> inputNumVec[i];
sort(inputNumVec.begin(), inputNumVec.end());
int maxCnt = , tmpNum;
for(int i = ; i <= inputNumVec.size()-maxCnt; ++ i)
{
tmpNum = getMaxCnt(i)-i+;
if(tmpNum > maxCnt)
maxCnt = tmpNum;
}
cout << maxCnt;
return ;
}
A 1086 Tree Traversals Again
自己想个办法建树之后进行后续遍历即可
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <stack> using namespace std;
typedef long long LL;
#define NoAddr -1
typedef struct NODE
{
int val, lChild, rChild;
NODE(int v):val(v),lChild(NoAddr),rChild(NoAddr){}
}node;
vector<node> nodeVec;
stack<int> valVec;
bool coutFlag = false;
void postOrder(int tmpRoot)
{
if(tmpRoot >= nodeVec.size())
return;
if(nodeVec[tmpRoot].lChild > )
postOrder(nodeVec[tmpRoot].lChild);
if(nodeVec[tmpRoot].rChild > )
postOrder(nodeVec[tmpRoot].rChild);
coutFlag ? printf(" ") :coutFlag = true;
cout << nodeVec[tmpRoot].val;
}
int main()
{
int N, tmpNum, lastIndex = ;
string tmpStr;
bool pushFlag = true;
cin >> N;
while(N > )
{
cin >> tmpStr;
if(tmpStr == "Push")
{
cin >> tmpNum;
valVec.push(nodeVec.size());
nodeVec.push_back(NODE(tmpNum));
pushFlag ? nodeVec[lastIndex].lChild = nodeVec.size()- :
nodeVec[lastIndex].rChild = nodeVec.size()- ;
lastIndex = nodeVec.size()-;
pushFlag = true;
}
else
{
lastIndex = valVec.top();
valVec.pop();
pushFlag = false;
N--;
}
}
postOrder();
return ;
}
A 1087 All Roads Lead to Rome
1.城市名string 和 序号index的转换
2.Dijkstra找最短距离
3.DFS根据题目中给出的优先级找到所需的条件
#include <cstdio>
#include <stdlib.h>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <stack>
#include <unordered_map>
using namespace std;
#define NoAddr -1
#define MAX_CITY 210
#define CLR(a,b) memset(a,b,sizeof(a))
typedef long long LL;
const int INF = 0x7f7f7f7f;
int N, K;
string citySt;
int route[MAX_CITY][MAX_CITY]={};
vector<int> preVec[MAX_CITY];
vector<int> tmpPath;
vector<int> outPath;
vector<int> disVec;
vector<int> cityHappyValVec;
int minCityCnt = MAX_CITY, maxHappy = -, minPathCnt = , tmpMaxHappy = ;
unordered_map<string, int> cityToIndexMap;
unordered_map<int, string> indexToCityMap;
void Dijkstra(int u)
{
vector<bool> visitFlagVec(N, false);
disVec.resize(N, INF);
disVec[u] = ;
for(int i = ; i < N; ++ i)
{
int minDis = INF, v = -;
for(int j = ; j < N; ++ j)
if(!visitFlagVec[j] && disVec[j] < minDis)
{
minDis = disVec[j];
v = j;
}
if(v == -)
return;
visitFlagVec[v] = true;
for(int j = ; j < N; ++ j)
{
if(!visitFlagVec[j] && disVec[v] + route[v][j] < disVec[j])
{
disVec[j] = disVec[v] + route[v][j];
preVec[j].clear();
preVec[j].push_back(v);
}
else if(!visitFlagVec[j] && disVec[v] + route[v][j] == disVec[j])
{
preVec[j].push_back(v);
}
}
}
}
void dfs(int u)
{
if(u == )
{
minPathCnt ++;
tmpPath.push_back();
if(tmpMaxHappy > maxHappy)
{
maxHappy = tmpMaxHappy;
outPath = tmpPath;
minCityCnt = tmpPath.size();
}
else if(tmpMaxHappy == maxHappy && tmpPath.size() < minCityCnt)
{
outPath = tmpPath;
}
tmpPath.pop_back();
}
tmpPath.push_back(u);
tmpMaxHappy += cityHappyValVec[u];
for(int i = ; i < preVec[u].size(); ++ i)
dfs(preVec[u][i]);
tmpMaxHappy -= cityHappyValVec[u];
tmpPath.pop_back();
}
int main()
{
cin >> N >> K >> citySt;
cityHappyValVec.resize(N, );
string tmpCity, tmpSt, tmpEnd;
int tmpHappy, tmpDis, stIndex, endIndex, endCityIndex;
cityToIndexMap[citySt] = ;
indexToCityMap[] = citySt;
for(int i = ; i < N; ++ i)
{
cin >> tmpCity >> tmpHappy;
cityHappyValVec[i] = tmpHappy;
cityToIndexMap[tmpCity] = i;
indexToCityMap[i] = tmpCity;
if(tmpCity == "ROM")
endCityIndex = i;
}
CLR(route,0x7f);
while(K--)
{
cin >> tmpSt >> tmpEnd >> tmpDis;
stIndex = cityToIndexMap[tmpSt];
endIndex = cityToIndexMap[tmpEnd];
route[stIndex][endIndex] = tmpDis;
route[endIndex][stIndex] = tmpDis;
}
Dijkstra();
dfs(endCityIndex);
cout << minPathCnt << " " << disVec[endCityIndex] << " " << maxHappy << " " << (int)maxHappy/(outPath.size()-) << endl;
bool symbolFlag = false;
for(int i = outPath.size()-; i >= ; -- i)
{
symbolFlag ? printf("->") : symbolFlag = true;
cout << indexToCityMap[outPath[i]];
}
return ;
}
PAT 2014 秋的更多相关文章
- 2014秋C++ 第8周项目 分支程序设计
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703.课程资源在云学堂"贺老师课堂"同步展示,使用的帐号请到课 ...
- PAT 2019 秋
考试的还行.不过略微有点遗憾,但是没想到第一题会直接上排序和dfs,感觉这次的题目难度好像是倒着的一样.哈哈哈哈. 第一题实在是搞崩心态,这道题给我的感觉是,可以做,但事实上总是差点啥. 第二题,第三 ...
- 2014秋C++ 第9周项目 循环程序设计
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703.课程资源在云学堂"贺老师课堂"同步展示,使用的帐号请到课 ...
- 2014秋C++第5周项目1參考-见识刚開始学习的人常见错误
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,实践要求见http://blog.csdn.net/sxhelijian/a ...
- 2014秋C++ 第7周项目 数据类型和表达式
课程主页在http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在云学堂"贺老师课堂"同步展示,使用的帐号请到课 ...
- PAT 2018 秋
A 1148 Werewolf - Simple Version 思路比较直接:模拟就行.因为需要序列号最小的两个狼人,所以以狼人为因变量进行模拟. #include <cstdio> # ...
- PAT 2011 秋
A : World Cup Betting #include <cstdio> #include <iostream> #include <algorithm> u ...
- 【2014】【】辛星【php】【秋】【1】php构建开发环境
**************************什么是开发环境*********************** 1.我们学习PHP,是使用它来做web用的,通俗理解,就是做站点. 2.站点的执行须要 ...
- 浙大PAT考试1077~1080(2014上机复试题目)
题目地址:点击打开链接 还是太弱. . 英文太差.,, 预计要等待被虐了.. 1077 找最长的公共后缀,暴力就能够写: #include<iostream> #include<cs ...
随机推荐
- Kubernetes 各版本镜像列表
以下镜像列表由 kubeadm v1.11.1 导出,若使用预下载镜像离线部署的方式部署,请使用 kubeadm v1.11.1 版本 导出各版本镜像列表: kubeadm config images ...
- 微信小程序语音(A)发给别人(B),也能播放,是需要先把语音上传到自己的服务器上才可以
小程序语音(A)发给别人(B),也能播放,是需要先把语音上传到自己的服务器上才可以. https://developers.weixin.qq.com/miniprogram/dev/api/medi ...
- Card Stacking 队列模拟
题目链接:https://ac.nowcoder.com/acm/contest/993/ABessie is playing a card game with her N-1 (2 <= N ...
- Centos7搭建SVN服务
1.安装 subversion [root@CentOS var]# yum -y install subversion 2.创建 svn 版本库,初始化相关配置文件 [root@CentOS v ...
- P1048 数字加密
P1048 数字加密 转跳点:
- Gym - 101158C Distribution Center
题意:n个传送带,传送带i运送编号为i的物品,机器人可以负责把传送带i上的物品放到传送带i + 1上,也可以把传送带i + 1上的物品放到传送带i上,机器人分布在传送带上x轴的不同位置,问每个传送带最 ...
- 法国:5G网络不会排除任何设备厂商
腾讯科技讯,据国外媒体报道,法国财政部长布鲁诺·勒梅尔(Bruno Le Maire)日前表示,法国有关 5G 电信网络的决定将基于网络的安全和性能,他强调说,法国政府不会将某一个特定的厂商排除在法国 ...
- 51nod 1276:岛屿的数量 很好玩的题目
1276 岛屿的数量 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 取消关注 有N个岛连在一起形成了一个大的岛屿,如果海平 ...
- JS 三大难点
1,作用域链 2,原型链 3,闭包
- Linux 文件夹和文件大小排序
Linux 文件夹和文件大小排序 文件夹排序 du -k | sort -rn 文件排序 ls -lS -r, –reverse 依相反次序排列 -R, –recursive 同时列出所有子目录层 - ...