Link: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 |
AC code:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#define LL long long
#define MAXN 100010
using namespace std;
int n;
string fun[20002];
int hh[20002][2],mm[20002][2],ss[20002][2];
int th[20002],tm[20002],ts[20002];
int sss[20002][2];
int sta[20002],vis[20002];
int top,fg,cnt,pre;
map<string,int>id;
int main()
{
//freopen("D:\\in.txt","r",stdin);
string s1,s2;
int i,h,m,s;
while(scanf("%d",&n)!=EOF)
{
//map<string,int>id;
id.clear();
top=0;
fg=1;
cnt=0;
for(i=1;i<=n;i++)
{
cin>>s1;
scanf("%d:%d:%d",&h,&m,&s);
cin>>s2;
if(s2=="START")
{
if(id[s1]!=0)
{
fg=0;
}
fun[++cnt]=s1;
hh[cnt][0]=h;
mm[cnt][0]=m;
ss[cnt][0]=s;
id[s1]=cnt;
sta[++top]=cnt;
sss[cnt][0]=h*3600+m*60+s;
if(i!=1)
{
if(pre>sss[cnt][0])
{
fg=0;
}
}
pre=sss[cnt][0];
}
else if(s2=="END")
{
if(id[s1]==0)
{
fg=0;
}
if(top==0||sta[top]!=id[s1])
{
fg=0;
}
else
{
hh[id[s1]][1]=h;
mm[id[s1]][1]=m;
ss[id[s1]][1]=s;
sss[id[s1]][1]=h*3600+m*60+s;
if(i!=1)
{
if(pre>sss[id[s1]][1])
{
fg=0;
}
}
pre=sss[id[s1]][1];
if(sss[id[s1]][1]<sss[id[s1]][0])
{
fg=0;
}
else
{
int ssss=sss[id[s1]][1]-sss[id[s1]][0];
th[id[s1]]=ssss/3600;
ssss-=th[id[s1]]*3600;
tm[id[s1]]=ssss/60;
ssss-=tm[id[s1]]*60;
ts[id[s1]]=ssss;
}
if(top==0)
{
fg=0;
}
else
{
top--;
}
}
}
}
if(!fg)
{
printf("Incorrect performance log\n");
}
else
{
for(i=1;i<=cnt;i++)
{
cout<<fun[i]<<" ";
if(th[i]<10)
{
printf("0%d:",th[i]);
}
else
{
printf("%d:",th[i]);
}
if(tm[i]<10)
{
printf("0%d:",tm[i]);
}
else
{
printf("%d:",tm[i]);
}
if(ts[i]<10)
{
printf("0%d\n",ts[i]);
}
else
{
printf("%d\n",ts[i]);
}
}
}
}
return 0;
}