题目地址:http://hihocoder.com/problemset/problem/1057
-
8 FuncA 00:00:01 START FuncB 00:00:02 START FuncC 00:00:03 START FuncC 00:00:04 END FuncB 00:00:05 END FuncD 00:00:06 START FuncD 00:00:07 END FuncA 00:00:08 END
样例输出
-
FuncA 00:00:07 FuncB 00:00:03 FuncC 00:00:01 FuncD 00:00:01
描述
You are given a txt file, which is performance logs of a single-threaded program.
Each line has three columns as follow:
[Function Name] [TimeStamp] [Action]
[FunctionName] is a string of length between 1~255
[TimeStamp] format is hh:mm:ss
Valid values for "Action" column are START or END, marking the start or end of a function call.
Each function will only be called once.
Output the depth-first traversal result of the call graph with the total time of each function call. However, sometimes the performance log isn't correct and at that time you just need to output "Incorrect performance log".
输入
The input only contains 1 case, first line is a positive number N representing the number of logs(1 <= N <= 20000), then there are N lines in next, each line is the log info containing [Function Name] [TimeStamp] [Action], [Function Name] is a string, you can assume the [Function Name] is distinct and the length between 1~255.
输出
Output the depth-first traversal result of the call graph with the total time of each function call for the correct performance, or output "Incorrect performance log".
提示
A call graph is a directed graph that represents calling relationships between subroutines in a computer program.
Call graph for the sample input is shown as below:
Another sample test case.
Sample Input | Sample Output |
8 FuncA 00:00:01 START FuncB 00:00:02 START FuncC 00:00:03 START FuncA 00:00:04 END FuncB 00:00:05 END FuncD 00:00:06 START FuncD 00:00:07 END FuncC 00:00:08 END |
Incorrect performance log |
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Stack;public class Main {public static void main(String[] args) {Scanner in=new Scanner(System.in);int n=in.nextInt();Stack<String> st=new Stack<String>();Map<String,myTime> map=new HashMap<String,myTime>();String[] s=new String[n+2];int smk=0,flag=0;while(n-->0){String name=in.next();String ts=in.next();String ac=in.next();if(ac.equals("START")){if(map.containsKey(name)){flag=1;}else{ //未包括map.put(name, new myTime(ts));st.push(name);s[smk++]=name;}}else if(ac.equals("END")){myTime mm=new myTime(ts);if(name.equals(st.peek())&&mm.compare(map.get(name))){myTime temp=map.get(name);map.remove(name);map.put(name, mm.sub(temp));st.pop();}else{flag=1;}}}if(flag==1||!st.isEmpty()){System.out.println("Incorrect performance log");}else{for(int i=0;i<smk;i++){System.out.println(s[i]+" "+map.get(s[i]).toString());}}}}
class myTime{private int second;private int minute;private int hour;public myTime(String s) {second=(int)s.charAt(7)-48+((int)s.charAt(6)-48)*10;minute=(int)s.charAt(4)-48+((int)s.charAt(3)-48)*10;hour=(int)s.charAt(1)-48+((int)s.charAt(0)-48)*10;}public myTime(int h,int m,int s) {second=s;minute=m;hour=h;}myTime sub(myTime mt){int msum=hour*3600+minute*60+second;int ssum=mt.hour*3600+mt.minute*60+mt.second;int s=(msum-ssum)%60;int m=(msum-ssum-s)%3600/60;int h=(msum-ssum)/3600;return new myTime(h,m,s);}public String toString(){String s="";s=s+ (char)(hour/10+48) +(char)(hour%10+48)+":";s=s+ (char)(minute/10+48) +(char)(minute%10+48)+":";s=s+ (char)(second/10+48) +(char)(second%10+48);return s;}boolean compare(myTime mt){if(mt.hour*3600+mt.minute*60+mt.second>hour*3600+minute*60+second) return false;return true;}
}
注意点(返回Incorrect performance log的情况):
1.每一个程序只能运行一遍
2.程序的开始可关闭都要满足栈的关系,即后开始的必须先关闭
3.程序的结束时间可能不大于开始时间
4.所有都处理完之后,栈必须是空的。