hihocoder 1057 : Performance Log

来源:互联网 发布:万国数据是外企吗 编辑:程序博客网 时间:2024/06/05 08:56

原题地址

分析地址

描述

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 InputSample Output8
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 ENDIncorrect performance log









样例输入
8FuncA 00:00:01 STARTFuncB 00:00:02 STARTFuncC 00:00:03 STARTFuncC 00:00:04 ENDFuncB 00:00:05 ENDFuncD 00:00:06 STARTFuncD 00:00:07 ENDFuncA 00:00:08 END
样例输出
FuncA 00:00:07FuncB 00:00:03FuncC 00:00:01FuncD 00:00:01
#include<stdio.h>#include<stdlib.h>#include<iostream>#include<math.h>#include<vector>#include<string>#include<sstream>#include<algorithm>#include<stack>#include<queue>#include<limits.h>#include<numeric>#include<cstring>#include<map>#include<set>using namespace std;#define eps 1e-8#define LL long longconst LL mod=12357;const int MIN= -1e3;const int MAX= 128 ;const int MAX_N = 2e4 + 10;const int MAX_M = 1e5 + 10;const int MAX_K = 1e4+10;static int N, M, K, T;struct fun{string name;string time;string status;long long difftime;}F[MAX_N];struct mytime{int hour;int min;int sec;};int timeToInt(string time) {int t[3];int index =0;int begin = 0;for(int i =0; i<time.size(); ++i) {if(time[i] == ':'){//const char* tmp = time.substr(begin, i-begin).c_str();//t[index ++] = atoi(tmp);t[index ++] = atoi(time.substr(begin, time.size()).c_str());begin = i+1;}}t[index ] = atoi(time.substr(begin, time.size()).c_str());mytime ret;long long count = 0;count = t[0]*24 * 60 * 60 + t[1] * 60 + t[2];return count;};long long  time_cmp(string time1, string time2) {long long  t1 = timeToInt(time1);long long t2 = timeToInt(time2);return (t1 - t2);};int main() {cin >>N ;getchar();stack <fun> sta;vector<fun> outputlist;for(int i =0; i<N; ++i) {cin >> F[i].name;cin >> F[i].time;cin >> F[i].status;}for(int i =0; i < N; ++i) {if(i != 0){int difftime = time_cmp(F[i].time, F[i-1].time);if(difftime < 0) {cout <<"Incorrect performance log" << endl;return 0;}}if(F[i].status == "START"){sta.push(F[i]);outputlist.push_back(F[i]);}else {if(sta.empty()|| sta.top().name != F[i].name) {cout <<"Incorrect performance log" << endl;return 0;}fun top = sta.top();sta.pop();int difftime = time_cmp(F[i].time, top.time);if(difftime < 0){cout <<"Incorrect performance log" << endl;return 0;}for(int j =0; j<outputlist.size(); ++j) {if(outputlist[j].name == F[i].name){outputlist[j].difftime = difftime;break;}}}}if(!sta.empty()) {cout <<"Incorrect performance log" << endl;return 0;}for(int i =0; i< outputlist.size(); ++i) {int diff = outputlist[i].difftime;int s = diff %60; diff /=60;int m = diff %60; diff /=60;int h = diff ;//printf("%s %02d:%02d:%02d\n", outputlist[i].name, h ,m ,s);cout << outputlist[i].name;printf(" %02d:%02d:%02d\n", h ,m ,s);}system("pause");return 0;}


0 0
原创粉丝点击