Performance Log

一、题目 : Performance Log (来源:点击打开链接)

描述
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:


callgraph.png


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



















样例输入
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掉的代码

#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <map>
#include <regex>
using namespace std;

//用于计算两个时间差
string minusTime(const string &time1,const string &time2)
{
	size_t data1=(((time1[0]-'0')*10+time1[1]-'0')*60+(time1[3]-'0')*10+time1[4]-'0')*60+(time1[6]-'0')*10+time1[7]-'0';
	size_t data2=(((time2[0]-'0')*10+time2[1]-'0')*60+(time2[3]-'0')*10+time2[4]-'0')*60+(time2[6]-'0')*10+time2[7]-'0';
	data1-=data2;
	string str;
	str.push_back(data1/36000+'0');
	str.push_back((data1/3600)%10+'0');
	str.push_back(':');
	str.push_back((data1%3600)/600+'0');
	str.push_back(((data1%3600)/60)%10+'0');
	str.push_back(':');
	str.push_back((data1%60)/10+'0');
	str.push_back(data1%10+'0');

	return str;
}
int main()
{
	vector<vector<string>> data;//存数据
	vector<string> tmp;
	stack<string> st1,st2,st3;//三个栈分别存函数名、时间和起止状态
	map<string,string> m;//存对应函数的执行时间
	int n,i,j;
	bool flag=true;
	string fun,time,status;
	//regex r("[012][0123]([:][012345]\\d{2}){2}");
	cin>>n;
	//读入数据
	for(i=0;i<n;i++)
	{
		tmp.clear();
		cin>>fun>>time>>status;
		tmp.push_back(fun);
		tmp.push_back(time);
		tmp.push_back(status);
		data.push_back(tmp);
	}
	//判断调用是否合法
	for(i=0;i<n;i++)
	{
		//如果栈为空,则把数据压入栈
		if(st1.empty())
		{
			st1.push(data[i][0]);
			st2.push(data[i][1]);
			st3.push(data[i][2]);
		}
		else
		{//否则查看当前函数是否匹配
			if(data[i][0]==st1.top()&&data[i][2]=="END"&&st3.top()=="START")
			{//查看起止时间顺序是否正确,如果不正确则输出结果
				if(st2.top().compare(data[i][1])<=0)//&&regex_match(st2.top(),r)&&regex_match(data[i][1],r)
				{
					m[data[i][0]]=minusTime(data[i][1],st2.top());
					st1.pop(),st2.pop(),st3.pop();
				}
				else
				{
					flag=false;
					cout<<"Incorrect performance log"<<endl;
					break;
				}
			}
			else//如不匹配,将数据压入栈中
			{
				st1.push(data[i][0]);
				st2.push(data[i][1]);
				st3.push(data[i][2]);
			}
		}
	}
	//如果先前的匹对没有问题
	if(flag)
	{//判断调用顺序是否正确,如栈不为空,说明调用有问题
		if(!st1.empty())
		{
			cout<<"Incorrect performance log"<<endl;
		}
		else//若调用正确,打印结果
		{
			for(i=0;i<n;i++)
			{
				if(data[i][2]=="START")
					cout<<data[i][0]<<" "<<m[data[i][0]]<<endl;
			}
		}
	}

	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值