A 1140 Look-and-say Sequence

  简单模拟。可能要注意字符串第一个字符和最后一个字符的处理。

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector> using namespace std;
string numToString(int n)
{
string tmpStr;
int tmpNum;
while(n > )
{
tmpNum = n%;
n /= ;
tmpStr = (char)(tmpNum + '')+tmpStr;
}
return tmpStr;
}
int main()
{
int m, tmpNum;;
string tmpStr, rstStr;
char tmpC;
cin >> tmpStr >> m;
rstStr = tmpStr;
m--;
while(m--)
{
rstStr.clear();
tmpNum = ;
tmpC = 'A';
for(int i = ; i < tmpStr.size(); ++i)
{
if(tmpStr[i] != tmpC)
{
if(i > )
rstStr = rstStr + tmpC + numToString(tmpNum);
tmpNum = ;
tmpC = tmpStr[i];
}
else
tmpNum++;
if(i == tmpStr.size()-)
rstStr = rstStr + tmpC + numToString(tmpNum);
}
tmpStr = rstStr;
}
cout << rstStr;
return ;
}

A 1141 PAT Ranking of Institutions

  排序题目。成绩为了尽量无误差,先按A,B,T存储,最后计算并排序。

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector> using namespace std;
typedef struct SCHOOLINFO
{
int score;
int peopleCnt;
int AScore, BScore, TScore;
string id;
SCHOOLINFO():AScore(),BScore(),TScore(),peopleCnt(){}
SCHOOLINFO(string str):AScore(),BScore(),TScore(),peopleCnt(),id(str){}
}schoolinfo;
unordered_map<string, int> schoolFlagMap;
vector<schoolinfo> schoolDataVec;
bool cmp(schoolinfo a, schoolinfo b)
{
if(a.score != b.score)
return a.score > b.score;
else if(a.peopleCnt != b.peopleCnt)
return a.peopleCnt < b.peopleCnt;
else
return a.id < b.id;
}
void strToLower(string &tmpStr)
{
for(int i = ; i < tmpStr.size(); ++ i)
{
if(tmpStr[i] <= 'Z' && tmpStr[i] >= 'A')
tmpStr[i] = tmpStr[i]-'A'+'a';
}
}
int main()
{
int n, index = , tmpNum;
string tmpId, tmpSchool;
int tmpScore;
cin >> n;
while(n--)
{
cin >> tmpId >> tmpScore >> tmpSchool;
strToLower(tmpSchool);
if(schoolFlagMap[tmpSchool] == )
{
schoolFlagMap[tmpSchool] = index++;
schoolDataVec.push_back(SCHOOLINFO(tmpSchool));
}
tmpNum = schoolFlagMap[tmpSchool]-;
schoolDataVec[tmpNum].peopleCnt++;
if(tmpId[] == 'T')
schoolDataVec[tmpNum].TScore += tmpScore;
else if(tmpId[] == 'A')
schoolDataVec[tmpNum].AScore += tmpScore;
else
schoolDataVec[tmpNum].BScore += tmpScore;
}
for(auto it = schoolDataVec.begin(); it != schoolDataVec.end(); ++it)
it->score = it->AScore + it->BScore/1.5 + it->TScore*1.5;
sort(schoolDataVec.begin(), schoolDataVec.end(), cmp);
int tmpTWS = -, rankCnt = ;
cout << schoolDataVec.size() << endl;
for(int i = ; i < schoolDataVec.size(); ++i)
{
schoolinfo tmpSchool = schoolDataVec[i];
if(tmpSchool.score != tmpTWS)
rankCnt = i+;
cout << rankCnt << " " << tmpSchool.id << " " <<tmpSchool.score << " " <<tmpSchool.peopleCnt << endl;
tmpTWS = schoolDataVec[i].score;
}
return ;
}

A 1142 Maximal Clique

  读懂题目就行,写一个判断是否与所给的其它各点连通的函数。

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector> using namespace std;
#define MAX_SIZE 210
int route[MAX_SIZE][MAX_SIZE]={};
int Nv, Ne, M;
vector<int> cliqueVec;
bool judge(int u)
{
for(auto it = cliqueVec.begin(); it != cliqueVec.end(); ++it)
{
if(*it == u)
continue;
if(route[*it][u] == )
return false;
}
return true;
}
int main()
{
cin >> Nv >> Ne;
int tmpSt, tmpEnd, tmpCnt, tmpNum;
for(int i = ; i <= Ne; ++i)
{
cin >> tmpSt >> tmpEnd;
route[tmpSt][tmpEnd] = ;
route[tmpEnd][tmpSt] = ;
}
cin >> M;
while(M--)
{
cin >> tmpCnt;
bool dealFlag = false;
vector<int> visitFlag(Nv+, );
cliqueVec.resize(tmpCnt,);
for(int i = ; i < tmpCnt; ++i)
{
cin >> cliqueVec[i];
visitFlag[cliqueVec[i]] = ;
}
for(auto it = cliqueVec.begin(); it != cliqueVec.end(); ++it)
if(!judge(*it))
dealFlag = true;
if(dealFlag)
{
cout << "Not a Clique" << endl;
continue;
}
for(int i = ; i <= Nv; ++i)
if(visitFlag[i] == && judge(i))
dealFlag = true;
if(dealFlag)
{
cout << "Not Maximal" <<endl;
continue;
}
cout << "Yes" << endl;
}
return ;
}

A 1143 Lowest Common Ancestor

  这道题目,深刻理解树的前序遍历和BST的话,是道比较水的题目。

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <vector> using namespace std;
vector<int> treeKeyVec();
unordered_map<int, int> keyValMap;
int M, N;
void findLCA(int a, int b)
{
for(int i = ; i <N; ++i)
{
int tmpNum = treeKeyVec[i];
bool flag = false;
if(tmpNum == a)
printf("%d is an ancestor of %d.\n", a, b);
else if(tmpNum == b)
printf("%d is an ancestor of %d.\n", b, a);
else if(tmpNum < max(a,b) && tmpNum > min(a,b))
printf("LCA of %d and %d is %d.\n", a, b, tmpNum);
else
flag = true;
if(!flag)
return;
}
}
int main()
{
cin >> M >> N;
int tmpNum;
for(int i = ; i < N; ++i)
{
cin >> tmpNum;
keyValMap[tmpNum] = ;
treeKeyVec[i] = tmpNum;
}
int tmpNum1, tmpNum2;
for(int i = ; i < M; ++i)
{
cin >> tmpNum1 >> tmpNum2;
bool flag1 = true, flag2 = true;
if(keyValMap[tmpNum1] == )
flag1 = false;
if(keyValMap[tmpNum2] == )
flag2 = false;
if(!flag1 && !flag2)
printf("ERROR: %d and %d are not found.\n", tmpNum1, tmpNum2);
else if(!flag1)
printf("ERROR: %d is not found.\n", tmpNum1);
else if(!flag2)
printf("ERROR: %d is not found.\n", tmpNum2);
else
findLCA(tmpNum1, tmpNum2);
}
return ;
}

PAT 2018 春的更多相关文章

  1. 2018春招-今日头条笔试题-第四题(python)

    题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) #-*- coding:utf-8 -*- class Magic: ''' a:用于存储数组a b:用于存储数组b num:用于 ...

  2. 2018春招-今日头条笔试题-第三题(python)

    题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 本题的做法最重要的应该是如何拼出‘1234567890’,对于输入表达试获得对应的结果利用python内置函数eval ...

  3. 2018春招-今日头条笔试题-第二题(python)

    题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 利用深度优先搜索 #-*- coding:utf-8 -*- class DFS: ''' num:用于存储最后执行次 ...

  4. 2018春招-今日头条笔试题-第一题(python)

    题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 要想得到输入的数字列中存在相隔为k的数,可以将输入的数字加上k,然后判断其在不在输入的数字列中即可. #-*- cod ...

  5. 2018春招实习笔试面试总结(PHP)

    博主双非渣本计算机软件大三狗一枚,眼看着春招就要结束了,现将自己所经历的的整个春招做一个个人总结. 首先就是关于投递计划,博主自己整理了一份各大公司的春招信息,包括网申地址,开始时间,结束时间,以及自 ...

  6. 链家2018春招Java工程师编程题题解

    Light 题目描述 在小红家里面,有n组开关,触摸每个开关,可以使得一组灯泡点亮.现在问你,使用这n组开关,最多能够使得多少个灯泡点亮呢? 输入 第一行一个n,表示有n组开关.接下来n行,每行第一个 ...

  7. 爱奇艺2018春招Java工程师编程题题解

    字典序最大子序列 题目描述 对于字符串a和b,如果移除字符串a中的一些字母(可以全部移除,也可以一个都不移除)就能够得到字符串b我们就称b是a的子序列. 例如."heo"是&quo ...

  8. 2018春招-今日头条笔试题5题(后附大佬答案-c++版)

    1题目描述 在n个元素的数组中,找到差值为k的除重后的数字对 输入描述 第一行:n和k,n表示数字的个数,k表示差值 第二行:n个整数 输入样例 输入: 5 2 1 5 3 4 2 输出: 3 说明: ...

  9. 2018春招-美团后台开发方向编程题 (python实现)

    第一题:字符串距离 题目: 给出两个相同长度的由字符 a 和 b 构成的字符串,定义它们的距离为对应位置不同的字符的数量.如串”aab”与串”aba”的距离为 2:串”ba”与串”aa”的距离为 1: ...

随机推荐

  1. 网络OSI七层模型及各层作用 与 TCP/IP

    背景 虽然说以前学习计算机网络的时候,学过了,但为了更好地学习一些物联网协议(MQTT.CoAP.LWM2M.OPC),需要重新复习一下. OSI七层模型 七层模型,亦称OSI(Open System ...

  2. Ubuntu系统部署tomcat并启用JMX实战案例

    Ubuntu系统部署tomcat并启用JMX实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装JDK环境 1>.更换阿里云的软件源 [root@zabbix_g ...

  3. Java数组去重的方法

    //第一种方式:最开始想到的是利用Set集合的不可重复性进行元素过滤 public static Object[] oneClear(Object[] arr){  Set set = new Has ...

  4. 2-10 就业课(2.0)-oozie:13、14、clouderaManager的服务搭建

    3.clouderaManager安装资源下载 第一步:下载安装资源并上传到服务器 我们这里安装CM5.14.0这个版本,需要下载以下这些资源,一共是四个文件即可 下载cm5的压缩包 下载地址:htt ...

  5. vscode dart 插件 关闭自动注释

    vscode dart 插件 关闭自动注释 左下角设置 --> 搜索 Closing Labels --> 去掉勾选

  6. Day5 - G - The Unique MST POJ - 1679

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  7. Vmware 和 VisualSVN-Server端口冲突

    安装 VisualSVN-Server 时,发现他和 Vmware   在端口  443 冲突: 先把本地自启动的 Vmware 全部停止,并改成手工启动服务: 这样可以节省资源,再安装 svn服务时 ...

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-pause

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  9. 重装系统,新安装IDEA启动项目后,classnotfound:com.mysql.jdbc.Driver

    我最后解决是:这个Test connection会自动帮你下载的,但是如果中途一直叫你try again,甚至到后面点这个test connection有弹窗,但是单窗里面的选项你点击后没反应,我是直 ...

  10. LeetCode1046 最后一块石头的重量(贪心—Java优先队列简单应用)

    题目: 有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出两块最重的石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == ...