hiho_1057_performance_log
题目大意
给出一个函数调用日志,判断日志是否合法,且求出合法日志中函数调用的时间长度。
题目链接:performance log
题目分析
首先需要清除非法日志的几种情形:
(1)日志的时间戳不是按照时间递增的顺序
(2)函数A中调用函数B,而函数A先于函数B结束
(3)函数没有被START过,却出现了END
每次输入一个日志记录,则判断时间戳是否递增;
维护一个调用堆栈call_stack,如果有函数START,则入栈,如果有函数END,则必须为call_stack顶部的函数,否则非法;
维护一个 哈希表 call_count表示函数调用的次数,如果函数F 进行START, 则call_count[F] + 1,若函数F进行END,则call_count[F] - 1。如果出现call_count[F] < 0,则非法;
在函数START时候,需要记录其开始时间 用 vector> callSeq记录。函数加入call_stack时候,需要知道该函数调用的次序,因此call_stack为 stack> 其中string为函数名,int为该函数在callSeq中的索引,用于在函数出栈时候在callSeq中找到该函数并记录其执行时间。
实现
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std; unordered_map<string, int> call_count; //记录每个函数被调用的次数,START就加1,END就减1
stack<pair<string, int>> call_stack; //调用堆栈, string函数名,int为函数的本次调用在callSeq中的索引
vector<pair<string, int> > call_seq; //最后输出的<调用函数,函数时间>
int str2intSec(char* str_time) {
int h, m, s;
sscanf(str_time, "%d:%d:%d", &h, &m, &s);
return 3600 * h + 60 * m + s;
}
string int2strSec(int sec) {
int h = sec / 3600;
int m = sec % 3600 / 60;
int s = sec % 60;
char str_time[20];
sprintf(str_time, "%02d:%02d:%02d", h, m, s);
return string(str_time);
} int main() {
int n;
char function_name[260];
char timestamp[20];
char action[20];
scanf("%d", &n);
getchar();
bool invalid = false;
int last_timestamp = -1;
for (int i = 0; i < n; i++) {
scanf("%s %s %s", function_name, timestamp, action);
if (invalid)
continue;
int sec = str2intSec(timestamp);
if (sec < last_timestamp) { //sec == last_timestap,是合法的
invalid = true;
continue;
}
last_timestamp = sec;
if (strcmp(action, "START") == 0) {
call_seq.push_back(pair<string, int>(function_name, sec));
call_stack.push(pair<string, int>(string(function_name), call_seq.size()-1));
call_count[string(function_name)] ++;
}
else if(strcmp(action, "END") == 0){
if (call_stack.empty()) {
invalid = true;
}
else{
string last_func = call_stack.top().first;
if (strcmp(function_name, last_func.c_str()) != 0) {
invalid = true;
continue;
}
call_count[last_func] --;
if (call_count[last_func] < 0) {
invalid = true;
continue;
}
int begin_index = call_stack.top().second;
int last_sec = call_seq.at(begin_index).second;
call_seq.at(begin_index).second = sec - last_sec;
call_stack.pop();
}
}
else {
invalid = true;
}
}
if (!call_stack.empty())
invalid = true;
if (invalid) {
printf("Incorrect performance log\n");
}
else {
for (int i = 0; i < call_seq.size(); i++) {
printf("%s %s\n", call_seq[i].first.c_str(), int2strSec(call_seq[i].second).c_str());
}
}
return 0;
}
hiho_1057_performance_log的更多相关文章
随机推荐
- js调用MVC3自带js验证
验证: if ($(this).is("form")) { return $(this).validate().checkForm() ...
- JS获取url参数及url编码、解码
完整的URL由这几个部分构成:scheme://host:port/path?query#fragment ,各部分的取法如下: window.location.href:获取完整url的方法:,即s ...
- Software caused connection abort: socket write error
Exception in thread "main" java.net.SocketException: Software caused connection abort: soc ...
- centos 3d特效
说下我怎么实现的吧 1.现在新力得里搜“XGL”和“Compiz”,把相关软件安装好. 2.安装ndivid的glx驱动: sudo apt-get install nvidia-kernel-com ...
- 如何提高android串口kernel log等级
在 /device/qcom/common/rootdir/etc/init.qcom.rc write /proc/sys/kernel/printk "6 6 1 7" 第一 ...
- BZOJ 1927 星际竞速(最小费用最大流)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1927 题意:一个图,n个点.对于给出的每条边 u,v,w,表示u和v中编号小的那个到编号 ...
- [翻译]投影变换 Projection Transform (Direct3D 9)
你可以认为投影变换就是控制摄像机内部的一种方式.他可以类推为为摄像机选择一个漏字板.它是三种变换中最难懂的.本文只讨论以下的一些内容. 典型的投影变换就是缩放和透视投影.投影就变换把视椎转化为一个立方 ...
- 各种操作中心Operation Center一览
Operation Center在中国可能有很多种名称,例如指挥中心.运维室.总控中心等等,国外可能也有很多名称,不管名称如何,任何一个上规模得数据总心或者运维单位一般都有一个这样得中心,来负责所管理 ...
- oracle, create table, insufficient privileges
SQL> exec pro_gz_day_report; ORA-01031: insufficient privileges ORA-06512: at & ...
- [HDOJ5773]The All-purpose Zero(贪心,DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意:给n个数,其中0可以用任何数字代替,问如何替换0使整个数列中的LIS最长. 0可以用任何数 ...