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

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

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

#include <iostream>
#include <cstdio>
#include <map>
using namespace std; int main(){
freopen("in.txt", "r", stdin); int n; string st1, st2;
map<string, int> startmp, endmp;
cin>>n;
for(int i=0; i<n; ++i){
cin >> st1 >> st2;
if(startmp.find(st1) == startmp.end() ){
startmp[st1] = 1;
}else{
++startmp[st1];
}
if( endmp.find(st2) == endmp.end() ){
endmp[st2] = 1;
}else{
++endmp[st2];
}
}
for(auto i= startmp.begin(); i != startmp.end(); ++i){
if( endmp.find(i->first) == endmp.end() || endmp[i->first] +1 == startmp[i->first] ){
cout << i->first << " ";
break;
}
}
for(auto i= endmp.begin(); i != endmp.end(); ++i){
if(startmp.find(i->first) == startmp.end() || startmp[i->first] + 1 == endmp[i->first] ){
cout << i->first << " ";
}
}
cout << endl;
return 0;
}

  

题目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。

#include <iostream>
#include <cstdlib>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 100+5; struct Node{
string st;
int num;
}nd[maxn]; bool MyCmp(Node a, Node b){
if(a.st == b.st){
return a.num < b.num;
}
return a.st < b.st;
} int main(){
int n, tmp, j;
string s, s2;
cin >> n;
for(int i=0; i<n; ++i){
cin >> s;
for(j=0; j<s.length(); ++j){
if(s[j]>='0' && s[j]<='9'){
break;
}
}
nd[i].st = s.substr(0, j);
s2 = s.substr(j); tmp = 0;
for(int k=0; k<s2.length(); ++k){
tmp = 10*tmp + s2[k]-'0';
}
nd[i].num = tmp;
}
sort(nd, nd+n, MyCmp);
for(int i=0; i<n; ++i){
cout << nd[i].st << nd[i].num << endl;
}
return 0;
}

  

第三题:

题目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 来检测是否有环!!!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 100000 + 5;
int n, m, flag, vis[maxn];
vector<int> mp[maxn]; bool dfsCheck(int cur){
if(vis[cur]){
return true;
}
vis[cur] = 1;
for(int i=0; i<mp[cur].size(); ++i){
if(dfsCheck(mp[cur][i])){
return true;
}
}
vis[cur] = 0;
return false;
}
int main(){
freopen("in.txt", "r", stdin); int test, x, y;
scanf("%d", &test);
while(test--){
scanf("%d %d", &n, &m);
for(int i=1; i<=n; ++i){
mp[i].clear();
vis[i] = 0;
}
for(int i=0; i<m; ++i){
scanf("%d %d", &x, &y);
mp[x].push_back(y);
}
flag = 0;
for(int i=1; i<=n; ++i){
if(dfsCheck(i)){
flag = 1;
break;
}
}
if(flag){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}

  

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. Unity iOS混合开发界面切换思路

    Unity iOS混合开发界面切换思路 最近有很多博友QQ 私信 或则 留言联系我,请教iOS和Unity界面之前相互切换的问题,源代码就不私下发你们了,界面跳转功能的代码我直接贴到下面好了,顺带说i ...

  2. sql将查询的结果集一次性插入到表变量中

    sql代码: declare @Subject table (--题目表变量 SubjectID int, Question nvarchar(MAX), CorrectAnswer ), Expla ...

  3. c3p0连接数据库的3种方式

    c3p0连接数据库的3种方式,这里以mysql为例 1. 直接用set方法设置参数, 基本方法 ComboPooledDataSource dataSource = new ComboPooledDa ...

  4. 使用do{ } while(0)的好处

    经常看到好多程序,尤其是linux相关的,使用do{}while(0)的写法,很明显内部程序最多只能执行一次,这样写的原因是什么呢?个人认为主要的原因是,如果不使用do{}while(0),那么当一个 ...

  5. FunDA(2)- Streaming Data Operation:流式数据操作

    在上一集的讨论里我们介绍并实现了强类型返回结果行.使用强类型主要的目的是当我们把后端数据库SQL批次操作搬到内存里转变成数据流式按行操作时能更方便.准确.高效地选定数据字段.在上集讨论示范里我们用集合 ...

  6. 如何实现一个php框架系列文章【6】mysql数据库

    实现一个mysql数据库封装需要考虑的问题 使用方便性 采用直接sql语句操作方式.只要会写sql语句,那么将没有其他学习成本. uctphp框架提供的dba辅助封装类,用会之后将爱不释手. 使用前需 ...

  7. SpringMVC传值、转发、重定向例子

    练习接收页面参数值 使用request 使用@RequestParam注解 使用实体对象 练习向页面传出数据 使用HttpServletRequest和session 使用ModelAndView对象 ...

  8. Mac下启动和停止Mysql服务

    方法1. 启动Mysql服务   sudo /Library/StartupItems/MySQLCOM/MySQLCOM start   停止Mysql服务   sudo /Library/Star ...

  9. GJM : Unity3D HIAR -【 快速入门 】 八、开发云识别应用

    开发云识别应用 为了解决识别图片数量限制,以及上线应用不能动态修改识别图片和 AR 内容的问题,我们在 HiAR SDK for Unity 新版本(v1.1.x 及后续版本)中集成了云识别功能.本文 ...

  10. SQLServer修改表字段名称

    EXEC sp_rename 'TableName.[ColumnName]','ColumnNew','COLUMN'