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 ...
随机推荐
- jmeter里面Dug Sampler 和json提取器的用法
1.编写用户详情请求 2.查看结果树 一级一级往上查找父集 3.添加json提取器 步骤:点击[用户详情]请求->添加->后置处理器->json提取器 把查看结果树里面的JSON P ...
- 通过流量管理器和 Azure Functions(作为代理)为全球用户提供最靠近的认知服务(或自定义API)
本实战是一个中等复杂度的综合性实战,涉及到的内容有TrafficManager,AzureFunctions,域名/域名解析等几个内容. 本案例基础介绍: https://www.bilibili.c ...
- 我的C语言的新开端<graphics.h>
进一步接触C语言<graphics.h> #include<stdio.h> #include<graphics.h> #include<conio.h> ...
- MyBatis Generator 下划线转驼峰命名
MyBatis Generator配置文件--指定生成实体类使用实际的表列名作为实体类的属性名 table标签下的设置属性useActualColumnNames用于指定生成实体类时是否使用实际的列名 ...
- 在excel表格里,为所有数字添上绿色小三角
在excel表格里,为所有数字添上绿色小三角的方法有4种: 1. 为一个单元格添加:直接在单元格里添加一个英文的逗号 2. 为一列数据添加:选中要添加绿色小三角的列,选择 数据-->分列--&g ...
- CAN通讯基本设置
A节点pelican协议下,扩展帧 单滤波方式 A节点的接收滤波器 ID号 设置为 0x19881205 设置过程 (1) pelican模式设置 设置时钟分频寄存器CDR.7 =1 使SJA1000 ...
- POJ 3321:Apple Tree 树状数组
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22131 Accepted: 6715 Descr ...
- Python基础笔记:input()输入与数据类型转换
input就是个万能输入,不过input输入的元素都是以str形式保存的,如果要他作为一个整数的话,就需要进行数据类型转换. input的使用 name=input('please input you ...
- 一百零九、SAP的OO-ALV之三,屏幕绘制器的使用
一.在Screen页面,点击格式,会打开屏幕绘制器 二.点击定制控制,和PS一样画出一个显示区域的画布容器 三.双击之后,在弹出的属性页面写入一个名字,保存 四.激活屏幕后关闭 五.关闭屏幕绘制器之后 ...
- java枚举类(转)
转自: http://blog.sina.com.cn/s/blog_697b968901013ih1.html 这里主要讲解的是Java的枚举类型 什么是枚举? 以我的理解答:枚举是我们自己定义的一 ...