Hihocoder's latest algorithm competition 15
Link:http://hihocoder.com/contest/hihointerview24
Topic 1:boarding Passes
Time limit: 10000ms
Single point time limit: 1000ms
Memory Limit: 256MB
Describe
Long ago you took a crazy trips around the world. You can not remember which cities do you start and finish the trip. Luckily you has all the boarding passes. Can you find the "starting City" and ending city?
Input
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" capital letters)
It is guaranteed this there is always a valid solution.
Output
The starting and ending cities separated by a space.
Sample input
4
SFO HKG
PVG BOS
HKG ABC
ABC PVG
Sample output
SFO BOS
Simple topic, according to a set of ticket end, to determine the starting point of the whole trip.
"Like Hamiltonian graphs", but one point can be passed multiple times.
#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; }
Topic 2:sorting Photo Files
Time limit: 10000ms
Single point time limit: 1000ms
Memory Limit: 256MB
Describe
You have a lot of photos whose file names is like:
Beijing1
Beijing10
Beijing8
shanghai233
As can see, all names has exactly, parts:the string part and the number part. If you sort the files in lexicographical order "BEIJING10" would go before "Beijing8". You don't like that. You want to sort the files first in lexicographical order of the string, then in ascending order of the number part. Can you write a program to work it out?
Input
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, the number part have no leading zeroes and is no more than 1000000.
Output
The sorted files one per line.
Sample input
4
Beijing1
Beijing10
Beijing8
b233
Sample output
b233
Beijing1
Beijing8
Beijing10
Simple topic, dividing string and num, on its condition 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; }
Question three:
Topic 3:circle Detect
Time limit: 10000ms
Single point time limit: 1000ms
Memory Limit: 256MB
Describe
You is given a directed graph G which has N nodes and M directed edges. Your task is to detect whether it contains any circle.
Input
The first line contains an integer T denoting the number of test cases. (1 <= T <= 5)
For each test case the first line contains the integers n and M. (1 <= N, M <= 100000)
Then follows M lines. Each contains the integers u and v denoting there is a edge from U to v. (1 <= u, v <= N)
Output
For each test, the output "YES" or "NO" denoting whether there is a circle in the graph.
Sample input
2
5 5
1 2
2 3
4 5
5 4
5 S
3 2
1 2
2 3
Sample output
YES
NO
A simple topic that focuses on how to use the DFS detection ring.
Use Dfscheck to detect if there is a ring!!!
#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's latest algorithm competition 15