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. mysql创建数据库并设置字符集编码

    create database `mydb` character set utf8 collate utf8_general_ci;

  2. day04-Python运维开发基础(位运算、代码块、流程控制)

    # (7)位运算符: & | ^ << >> ~ var1 = 19 var2 = 15 # & 按位与 res = var1 & var2 " ...

  3. dubbo 相关面试题 有用(转)

    调用关系说明: · 0. 服务容器负责启动,加载,运行服务提供者. · 1. 服务提供者在启动时,向注册中心注册自己提供的服务. · 2. 服务消费者在启动时,向注册中心订阅自己所需的服务. · 3. ...

  4. Spring的JDBC的使用(配置和CRUD)

    导包: Spring的JDBC模板的使用 一.默认连接池 创建数据库 create database spring4; use spring4; create table account(id int ...

  5. 认识json,详解JsonConfig

    说到json 初学者很迷茫,不知json怎么为何物,以及怎么用.我简单说下我的了解 既然用了json 我们就要知其然也知其所以然.下面有几个疑问 1.为什么要用json?也就是json 的优势 2.我 ...

  6. 0101-ioc

    背景 ioc是spring的基础,即控制反转.springboot基于注解使用ioc. ioc spring称所有被管理的对象为bean, spring ioc主要通过描述的方式完成3类bean的管理 ...

  7. delphi http请求用到的编码方式

    uses HttpEncode: HttpEncode(AnsiToUtf8('***'))

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

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

  9. http 请求code状态码

    状态码 含义 100 客户端应当继续发送请求.这个临时响应是用来通知客户端它的部分请求已经被服务器接收,且仍未被拒绝.客户端应当继续发送请求的剩余部分,或者如果请求已经完成,忽略这个响应.服务器必须在 ...

  10. HihoCoder第十一周:树中的最长路

    #1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...