A 1140 Look-and-say Sequence

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

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8. string numToString(int n)
  9. {
  10. string tmpStr;
  11. int tmpNum;
  12. while(n > )
  13. {
  14. tmpNum = n%;
  15. n /= ;
  16. tmpStr = (char)(tmpNum + '')+tmpStr;
  17. }
  18. return tmpStr;
  19. }
  20. int main()
  21. {
  22. int m, tmpNum;;
  23. string tmpStr, rstStr;
  24. char tmpC;
  25. cin >> tmpStr >> m;
  26. rstStr = tmpStr;
  27. m--;
  28. while(m--)
  29. {
  30. rstStr.clear();
  31. tmpNum = ;
  32. tmpC = 'A';
  33. for(int i = ; i < tmpStr.size(); ++i)
  34. {
  35. if(tmpStr[i] != tmpC)
  36. {
  37. if(i > )
  38. rstStr = rstStr + tmpC + numToString(tmpNum);
  39. tmpNum = ;
  40. tmpC = tmpStr[i];
  41. }
  42. else
  43. tmpNum++;
  44. if(i == tmpStr.size()-)
  45. rstStr = rstStr + tmpC + numToString(tmpNum);
  46. }
  47. tmpStr = rstStr;
  48. }
  49. cout << rstStr;
  50. return ;
  51. }

A 1141 PAT Ranking of Institutions

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

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <string>
  5. #include <unordered_map>
  6. #include <vector>
  7.  
  8. using namespace std;
  9. typedef struct SCHOOLINFO
  10. {
  11. int score;
  12. int peopleCnt;
  13. int AScore, BScore, TScore;
  14. string id;
  15. SCHOOLINFO():AScore(),BScore(),TScore(),peopleCnt(){}
  16. SCHOOLINFO(string str):AScore(),BScore(),TScore(),peopleCnt(),id(str){}
  17. }schoolinfo;
  18. unordered_map<string, int> schoolFlagMap;
  19. vector<schoolinfo> schoolDataVec;
  20. bool cmp(schoolinfo a, schoolinfo b)
  21. {
  22. if(a.score != b.score)
  23. return a.score > b.score;
  24. else if(a.peopleCnt != b.peopleCnt)
  25. return a.peopleCnt < b.peopleCnt;
  26. else
  27. return a.id < b.id;
  28. }
  29. void strToLower(string &tmpStr)
  30. {
  31. for(int i = ; i < tmpStr.size(); ++ i)
  32. {
  33. if(tmpStr[i] <= 'Z' && tmpStr[i] >= 'A')
  34. tmpStr[i] = tmpStr[i]-'A'+'a';
  35. }
  36. }
  37. int main()
  38. {
  39. int n, index = , tmpNum;
  40. string tmpId, tmpSchool;
  41. int tmpScore;
  42. cin >> n;
  43. while(n--)
  44. {
  45. cin >> tmpId >> tmpScore >> tmpSchool;
  46. strToLower(tmpSchool);
  47. if(schoolFlagMap[tmpSchool] == )
  48. {
  49. schoolFlagMap[tmpSchool] = index++;
  50. schoolDataVec.push_back(SCHOOLINFO(tmpSchool));
  51. }
  52. tmpNum = schoolFlagMap[tmpSchool]-;
  53. schoolDataVec[tmpNum].peopleCnt++;
  54. if(tmpId[] == 'T')
  55. schoolDataVec[tmpNum].TScore += tmpScore;
  56. else if(tmpId[] == 'A')
  57. schoolDataVec[tmpNum].AScore += tmpScore;
  58. else
  59. schoolDataVec[tmpNum].BScore += tmpScore;
  60. }
  61. for(auto it = schoolDataVec.begin(); it != schoolDataVec.end(); ++it)
  62. it->score = it->AScore + it->BScore/1.5 + it->TScore*1.5;
  63. sort(schoolDataVec.begin(), schoolDataVec.end(), cmp);
  64. int tmpTWS = -, rankCnt = ;
  65. cout << schoolDataVec.size() << endl;
  66. for(int i = ; i < schoolDataVec.size(); ++i)
  67. {
  68. schoolinfo tmpSchool = schoolDataVec[i];
  69. if(tmpSchool.score != tmpTWS)
  70. rankCnt = i+;
  71. cout << rankCnt << " " << tmpSchool.id << " " <<tmpSchool.score << " " <<tmpSchool.peopleCnt << endl;
  72. tmpTWS = schoolDataVec[i].score;
  73. }
  74. return ;
  75. }

A 1142 Maximal Clique

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

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <string>
  5. #include <unordered_map>
  6. #include <vector>
  7.  
  8. using namespace std;
  9. #define MAX_SIZE 210
  10. int route[MAX_SIZE][MAX_SIZE]={};
  11. int Nv, Ne, M;
  12. vector<int> cliqueVec;
  13. bool judge(int u)
  14. {
  15. for(auto it = cliqueVec.begin(); it != cliqueVec.end(); ++it)
  16. {
  17. if(*it == u)
  18. continue;
  19. if(route[*it][u] == )
  20. return false;
  21. }
  22. return true;
  23. }
  24. int main()
  25. {
  26. cin >> Nv >> Ne;
  27. int tmpSt, tmpEnd, tmpCnt, tmpNum;
  28. for(int i = ; i <= Ne; ++i)
  29. {
  30. cin >> tmpSt >> tmpEnd;
  31. route[tmpSt][tmpEnd] = ;
  32. route[tmpEnd][tmpSt] = ;
  33. }
  34. cin >> M;
  35. while(M--)
  36. {
  37. cin >> tmpCnt;
  38. bool dealFlag = false;
  39. vector<int> visitFlag(Nv+, );
  40. cliqueVec.resize(tmpCnt,);
  41. for(int i = ; i < tmpCnt; ++i)
  42. {
  43. cin >> cliqueVec[i];
  44. visitFlag[cliqueVec[i]] = ;
  45. }
  46. for(auto it = cliqueVec.begin(); it != cliqueVec.end(); ++it)
  47. if(!judge(*it))
  48. dealFlag = true;
  49. if(dealFlag)
  50. {
  51. cout << "Not a Clique" << endl;
  52. continue;
  53. }
  54. for(int i = ; i <= Nv; ++i)
  55. if(visitFlag[i] == && judge(i))
  56. dealFlag = true;
  57. if(dealFlag)
  58. {
  59. cout << "Not Maximal" <<endl;
  60. continue;
  61. }
  62. cout << "Yes" << endl;
  63. }
  64. return ;
  65. }

A 1143 Lowest Common Ancestor

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

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <string>
  5. #include <unordered_map>
  6. #include <vector>
  7.  
  8. using namespace std;
  9. vector<int> treeKeyVec();
  10. unordered_map<int, int> keyValMap;
  11. int M, N;
  12. void findLCA(int a, int b)
  13. {
  14. for(int i = ; i <N; ++i)
  15. {
  16. int tmpNum = treeKeyVec[i];
  17. bool flag = false;
  18. if(tmpNum == a)
  19. printf("%d is an ancestor of %d.\n", a, b);
  20. else if(tmpNum == b)
  21. printf("%d is an ancestor of %d.\n", b, a);
  22. else if(tmpNum < max(a,b) && tmpNum > min(a,b))
  23. printf("LCA of %d and %d is %d.\n", a, b, tmpNum);
  24. else
  25. flag = true;
  26. if(!flag)
  27. return;
  28. }
  29. }
  30. int main()
  31. {
  32. cin >> M >> N;
  33. int tmpNum;
  34. for(int i = ; i < N; ++i)
  35. {
  36. cin >> tmpNum;
  37. keyValMap[tmpNum] = ;
  38. treeKeyVec[i] = tmpNum;
  39. }
  40. int tmpNum1, tmpNum2;
  41. for(int i = ; i < M; ++i)
  42. {
  43. cin >> tmpNum1 >> tmpNum2;
  44. bool flag1 = true, flag2 = true;
  45. if(keyValMap[tmpNum1] == )
  46. flag1 = false;
  47. if(keyValMap[tmpNum2] == )
  48. flag2 = false;
  49. if(!flag1 && !flag2)
  50. printf("ERROR: %d and %d are not found.\n", tmpNum1, tmpNum2);
  51. else if(!flag1)
  52. printf("ERROR: %d is not found.\n", tmpNum1);
  53. else if(!flag2)
  54. printf("ERROR: %d is not found.\n", tmpNum2);
  55. else
  56. findLCA(tmpNum1, tmpNum2);
  57. }
  58. return ;
  59. }

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. python编写的banner获取代码的两种方式

    1.无选项和帮助信息 #!/usr/bin/env python #coding:utf-8 import socket import sys import os from threading imp ...

  2. PAT (Advanced Level) 1124~1127:1124模拟 1125优先队列 1126欧拉通路 1127中序后序求Z字形层序遍历

    1124 Raffle for Weibo Followers(20 分) 题意:微博抽奖,有M个人,标号为1~M.从第S个人开始,每N个人可以获奖,但是已获奖的人不能重复获奖,需要跳过该人把机会留给 ...

  3. 使用JMX连接JVM

    什么是JMX? 什么是JMX,Java Management Extensions,即Java管理扩展,是一个为应用程序.设备.系统等植入管理功能的框架.JMX可以跨越一系列异构操作系统平台.系统体系 ...

  4. maven详解之 pom.xml 解释

    <project>:pom.xml的根标签,一个maven项目用一对<peoject></project>标签包裹. <modelVersion>4.0 ...

  5. 数据库建模工具pd的使用

  6. mysql limit查询入门

  7. Ajax学习系列——向服务器发送请求

    1.如何发送请求? 如果需要向服务器发送请求,我们使用的是XMLHttpRequest对象中的open()和send()方法. var xhr = new XMLHttpRequest();//具体创 ...

  8. python基础(变量,字符串,列表,元组)

    #列表的操作list1 = ['wuqiang','lichang','changhao'] #列表的定义print(list1) #操作列表print(list1[-1]) #操作列表的最后一位li ...

  9. Node.js 发送Email

    章节 Node.js 介绍 Node.js 入门 Node.js 模块 Node.js HTTP模块 Node.js 文件系统模块 Node.js URL模块 Node.js NPM Node.js ...

  10. hdu 1160 上升序列 dp

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...