#1057 : Performance Log 线程日志(栈模拟)


Link:http://hihocoder.com/problemset/problem/1057#


时间限制: 8000ms
单点时限: 1000ms
内存限制: 256MB

描述

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 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









样例输入
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


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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值