hihoCoder太阁最新面经算法竞赛15

Link: http://hihocoder.com/contest/hihointerview24

题目1 : Boarding Passes

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

 

描述

Long long ago you took a crazy trip around the world. You can not remember which cities did you start and finish the trip.  Luckily you have all the boarding passes. Can you find out the starting city and ending city?

输入

The first line contains a number N denoting the number of boarding passes.  (1 <= N <= 100)

The following N lines each contain a pair of cities (the city name consists of no more than 10 capital letters)

It is guaranteed that there is always a valid solution.

输出

The starting and ending cities separated by a space.

样例输入

4

SFO HKG

PVG BOS

HKG ABC

ABC PVG

样例输出

SFO BOS

简单的题目, 根据一组车票的起终点,来判断整个行程的起始点。

“类似哈密顿图”, 不过一个点可以通过多次。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <map>
  4. using namespace std;
  5.  
  6. int main(){
  7. freopen("in.txt", "r", stdin);
  8.  
  9. int n; string st1, st2;
  10. map<string, int> startmp, endmp;
  11. cin>>n;
  12. for(int i=0; i<n; ++i){
  13. cin >> st1 >> st2;
  14. if(startmp.find(st1) == startmp.end() ){
  15. startmp[st1] = 1;
  16. }else{
  17. ++startmp[st1];
  18. }
  19. if( endmp.find(st2) == endmp.end() ){
  20. endmp[st2] = 1;
  21. }else{
  22. ++endmp[st2];
  23. }
  24. }
  25. for(auto i= startmp.begin(); i != startmp.end(); ++i){
  26. if( endmp.find(i->first) == endmp.end() || endmp[i->first] +1 == startmp[i->first] ){
  27. cout << i->first << " ";
  28. break;
  29. }
  30. }
  31. for(auto i= endmp.begin(); i != endmp.end(); ++i){
  32. if(startmp.find(i->first) == startmp.end() || startmp[i->first] + 1 == endmp[i->first] ){
  33. cout << i->first << " ";
  34. }
  35. }
  36. cout << endl;
  37. return 0;
  38. }

  

题目2 : Sorting Photo Files

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

 

描述

You have a lot of photos whose file names are like:

beijing1

beijing10

beijing8

shanghai233

As you can see, all names have exactly two parts: the string part and the number part. If you sort the files in lexicographical order "beijing10" will go before "beijing8". You do not like that. You want to sort the files first in lexicographical order of the string part then in ascending order of the number part. Can you write a program to work it out?

输入

The first line contains an integer N denoting the number of files. (1 <= N <= 100)

The following N lines each contain a file name as described above.

It is guaranteed that the number part has no leading zeroes and is no more than 1000000.

输出

The sorted files one per line.

样例输入

4

beijing1

beijing10

beijing8

b233

样例输出

b233

beijing1

beijing8

beijing10

简单的题目, 划分string和num, 对其条件sort。

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6. const int maxn = 100+5;
  7.  
  8. struct Node{
  9. string st;
  10. int num;
  11. }nd[maxn];
  12.  
  13. bool MyCmp(Node a, Node b){
  14. if(a.st == b.st){
  15. return a.num < b.num;
  16. }
  17. return a.st < b.st;
  18. }
  19.  
  20. int main(){
  21. int n, tmp, j;
  22. string s, s2;
  23. cin >> n;
  24. for(int i=0; i<n; ++i){
  25. cin >> s;
  26. for(j=0; j<s.length(); ++j){
  27. if(s[j]>='0' && s[j]<='9'){
  28. break;
  29. }
  30. }
  31. nd[i].st = s.substr(0, j);
  32. s2 = s.substr(j); tmp = 0;
  33. for(int k=0; k<s2.length(); ++k){
  34. tmp = 10*tmp + s2[k]-'0';
  35. }
  36. nd[i].num = tmp;
  37. }
  38. sort(nd, nd+n, MyCmp);
  39. for(int i=0; i<n; ++i){
  40. cout << nd[i].st << nd[i].num << endl;
  41. }
  42. return 0;
  43. }

  

第三题:

题目3 : Circle Detect

时间限制:10000ms

单点时限:1000ms

内存限制:256MB

 

描述

You are given a directed graph G which has N nodes and M directed edges. Your task is to detect whether it contains any circle.

输入

The first line contains an integer T denoting the number of test cases. (1 <= T <= 5)

For each test case the first line contains two integers N and M. (1 <= N, M <= 100000)

Then follows M lines. Each contains two integers u and v denoting there is an edge from u to v. (1 <= u, v <= N)

输出

For each test case output "YES" or "NO" denoting whether there is a circle in the graph.

样例输入

2

5 5

1 2

2 3

4 5

5 4

4 2

3 2

1 2

2 3

样例输出

YES

NO

简单的题目, 重点在于如何使用dfs检测环。

使用dfsCheck 来检测是否有环!!!

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. using namespace std;
  6. const int maxn = 100000 + 5;
  7. int n, m, flag, vis[maxn];
  8. vector<int> mp[maxn];
  9.  
  10. bool dfsCheck(int cur){
  11. if(vis[cur]){
  12. return true;
  13. }
  14. vis[cur] = 1;
  15. for(int i=0; i<mp[cur].size(); ++i){
  16. if(dfsCheck(mp[cur][i])){
  17. return true;
  18. }
  19. }
  20. vis[cur] = 0;
  21. return false;
  22. }
  23. int main(){
  24. freopen("in.txt", "r", stdin);
  25.  
  26. int test, x, y;
  27. scanf("%d", &test);
  28. while(test--){
  29. scanf("%d %d", &n, &m);
  30. for(int i=1; i<=n; ++i){
  31. mp[i].clear();
  32. vis[i] = 0;
  33. }
  34. for(int i=0; i<m; ++i){
  35. scanf("%d %d", &x, &y);
  36. mp[x].push_back(y);
  37. }
  38. flag = 0;
  39. for(int i=1; i<=n; ++i){
  40. if(dfsCheck(i)){
  41. flag = 1;
  42. break;
  43. }
  44. }
  45. if(flag){
  46. printf("YES\n");
  47. }else{
  48. printf("NO\n");
  49. }
  50. }
  51. return 0;
  52. }

  

hihoCoder太阁最新面经算法竞赛15的更多相关文章

  1. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

  2. hihocoder Round #c1(hihoCoder太阁最新面经算法竞赛1 )

    Test链接:https://cn.vjudge.net/contest/231849 选自hihoCoder太阁最新面经算法竞赛1 更多Test:传送门 A:区间求差 给一组区间集合A和区间集合B, ...

  3. hihoCoder太阁最新面经算法竞赛19

    比赛链接:http://hihocoder.com/contest/hihointerview28/problems A. 固定一个方向,两两相邻的点顺时针或逆时针构造三个向量,判断这个点在这个向量的 ...

  4. hihoCoder太阁最新面经算法竞赛18

    比赛链接:http://hihocoder.com/contest/hihointerview27/problems A.Big Plus 模拟水 #include <bits/stdc++.h ...

  5. hihoCoder太阁最新面经算法竞赛17

    比赛链接:http://hihocoder.com/contest/hihointerview26 A.排序后枚举两个点,确定一个矩形后二分剩下两个点. #include <bits/stdc+ ...

  6. [HIHO]hihoCoder太阁最新面经算法竞赛7

    题目链接:http://hihocoder.com/contest/hihointerview12 期末完事了,终于有时间成套刷题了.这套题比较简单,难度上感觉和上一套差不多.除了最后一个题是看了讨论 ...

  7. zz 圣诞丨太阁所有的免费算法视频资料整理

    首发于 太阁实验室 关注专栏   写文章     圣诞丨太阁所有的免费算法视频资料整理 Ray Cao· 12 小时前 感谢大家一年以来对太阁实验室的支持,我们特地整理了在过去一年中我们所有的原创算法 ...

  8. [刷题]算法竞赛入门经典 3-12/UVa11809

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 题目:算法竞赛入门经典 3-4/UVa11809:Floating-Point Numbers 代码: //UVa11 ...

  9. [刷题]算法竞赛入门经典 3-1/UVa1585 3-2/UVa1586 3-3/UVa1225

    书上具体所有题目:http://pan.baidu.com/s/1hssH0KO(我也是在网上找到的pdf,但不记得是从哪里搜刮到的了,就重新上传了一遍) PS:第一次写博客分享我的代码,不知道我对c ...

随机推荐

  1. 基于Metronic的Bootstrap开发框架经验总结(13)--页面链接收藏夹功能的实现2(利用Sortable进行拖动排序)

    在上篇随笔<基于Metronic的Bootstrap开发框架经验总结(12)--页面链接收藏夹功能的实现>上,我介绍了链接收藏夹功能的实现,以及对收藏记录的排序处理.该篇随笔主要使用功能按 ...

  2. Entity Framework 教程——模型浏览器

    模型浏览器: 在之前的章节中,我们创建了第一个关于学校的实体数据模型.但是EDM设计器并没有将他所创建的所有对象完全显示出来.它只将数据库中的被选择的表与视图显示出来了. 模型浏览器可以将EDM所创建 ...

  3. Razor 语法初级使用,不断更新此文章

    有兴趣的可以看看菜鸟教程的   http://www.runoob.com/aspnet/razor-cs-loops.html 1.ViewData展示登陆的Session信息 Controller ...

  4. 【转】MVC、MVP与MVT

    MVC是Model-View-Control的缩写,Model指的是数据层,View指的是UI层,Control指的是控制层,这三层之间彼此联系.View层的用户行为,触发Control层,Contr ...

  5. 一些简单的C语言算法

    1. 要求输入一个正整数,打印下述图形 输入:5 输出: * ** *** **** ***** 实现代码如下: #include <stdio.h> int main(int argc, ...

  6. JSON转化为JAVABEAN集合

    String str = "[{'id':'392','type':'jpg'},{'id':'393','type':'jpg'},{'id':'377','type':'jpg'}]&q ...

  7. Windows消息机制

    Windows的消息系统是由3个部分组成的: · 消息队列.Windows能够为所有的应用程序维护一个消息队列.应用程序必须从消息队列中获取消息,然后分派给某个窗口.· 消息循环.通过这个循环机制应用 ...

  8. HTML中的标记-遁地龙卷风

    第三版 上一版本在未经验证的情况下,盲目的认为很多东西是那样,造成错误,非常抱歉. 0.什么是标记 <input type="checkbox" checked />& ...

  9. Spring in Action 学习笔记一

    Spring 核心       Spring的主要特性仅仅是 依赖注入DI和面向切面编程AOP       JavaBean 1996.12 Javav 规范针对Java定义了软件组件模型,是简单的J ...

  10. ipython notebook 浏览器中编写数学公式和现实

    Python Notebook简介1 http://www.cnblogs.com/cbscan/p/3545084.html $ python -m IPython http://pypi.pyth ...