API请求日志去重分析华为OD机试真题 华为OD上机考试真题 4月15号 100分题型华为OD机试真题目录点击查看: 华为OD机试真题题库目录机考题库 算法考点详解题目描述某微服务系统的日志监控平台需要分析 API调用记录。日志中包含大量重复的请求记录为了优化存储和后续分析需要对相邻的重复请求进行合并统计。具体规则如下1.日志按时间顺序排列每条记录包含请求路径和响应时间2.如果连续出现相同的请求路径需要将这些记录合并为一条3.合并后的记录需要统计该路径连续出现的次数并保留所有响应时间的平均值4.相同路径但被其他路径分隔的视为不同的记录组,需要分别合并请实现一个函数对给定的日志数据进行去重合并处理。输入描述输入请求路径path数组按时间顺序排列输入对应的响应时间responseTimes数组(毫秒)补充0≤paths.length≤10^50≤responseTimes.length≤10^5paths.lengthresponseTimes.length1≤responseTimes[i]≤10^4路径长度不超过 100个字符输出描述按顺序输出每个记录组信息每个记录组包含以下三个元素该路径在输入数组中首次出现索引该路径连续出现的次数该组路径的平均响应时间(向下取整)用例1输入/api/user,/api/user,/api/order,/api/user,/api/order,/api/order 100,200,150,300,250,350输出0,2,150 2,1,150 3,1,300 4,2,300说明/api/user在索引0-1连续出现平均响应时间为150/api/order在索引2单独出现平均响应时间为150api/user在索引3单独出现由于被分割响应时间为300/api/order在索引 4−5 连续出现 2 次平均响应时间(250350)/2300题解思路双指针使用双指针统计每个相同请求地址的{起始位置终止位置}并累加计算区域内总响应时间。根据1的逻辑可以得出每条记录的{首次索引连续次数平均响应时间}按顺序保存在结果数组中返回结果数组即可c#includeiostream #includevector #includestring #include utility #include sstream #includealgorithm #includecmath #includemap using namespace std; // 通用 切割函数 函数 将字符串str根据delimiter进行切割 vectorstring split(const string str, const string delimiter) { vectorstring result; size_t start 0; size_t end str.find(delimiter); while (end ! string::npos) { result.push_back(str.substr(start, end - start)); start end delimiter.length(); end str.find(delimiter, start); } // 添加最后一个部分 result.push_back(str.substr(start)); return result; } // 统计函数 vectorvectorint hanle(vectorstring path, vectorint responseTime) { vectorvectorint res; int n path.size(); for (int i 0; i n; ) { int totalTime responseTime[i]; int j i; // 获取相同路径起始和终止连续位置 while (j 1 n path[j] path[j1]) { totalTime responseTime[j1]; j; } res.push_back({i, j - i 1, totalTime / (j - i 1)}); i j 1; } return res; } int main() { string pathInput; string responseInput; getline(cin, pathInput); getline(cin, responseInput); vectorstring paths split(pathInput, ,); vectorint responseTime(paths.size()); vectorstring tmp split(responseInput, ,); for (int i 0; i tmp.size(); i) { responseTime[i] stoi(tmp[i]); } vectorvectorint res hanle(paths, responseTime); // 输出结果 for (int i 0; i res.size(); i) { cout res[i][0] , res[i][1] , res[i][2]; if (i ! res.size() -1) { cout ; } } return 0; }JAVAimport java.util.*; public class Main { // 统计函数 static Listint[] hanle(ListString path, ListInteger responseTime) { Listint[] res new ArrayList(); int n path.size(); for (int i 0; i n;) { int totalTime responseTime.get(i); int j i; // 获取相同路径起始和终止连续位置 while (j 1 n path.get(j).equals(path.get(j 1))) { totalTime responseTime.get(j 1); j; } res.add(new int[]{i, j - i 1, totalTime / (j - i 1)}); i j 1; } return res; } public static void main(String[] args) { Scanner sc new Scanner(System.in); String pathInput sc.nextLine(); String responseInput sc.nextLine(); String[] pathsArr pathInput.split(,); String[] responseArr responseInput.split(,); ListString paths Arrays.asList(pathsArr); ListInteger responseTime new ArrayList(); for (String s : responseArr) { responseTime.add(Integer.parseInt(s)); } Listint[] res hanle(paths, responseTime); for (int i 0; i res.size(); i) { int[] r res.get(i); System.out.print(r[0] , r[1] , r[2]); if (i ! res.size() - 1) System.out.print( ); } } }Pythonimportsys# 统计函数defhanle(path,response_time):res[]nlen(path)i0whilein:total_timeresponse_time[i]ji# 获取相同路径起始和终止连续位置whilej1nandpath[j]path[j1]:total_timeresponse_time[j1]j1res.append([i,j-i1,total_time//(j-i1)])ij1returnresif__name____main__:path_inputsys.stdin.readline().strip()response_inputsys.stdin.readline().strip()pathspath_input.split(,)response_timelist(map(int,response_input.split(,)))reshanle(paths,response_time)print( .join(f{r[0]},{r[1]},{r[2]}forrinres))JavaScriptconstreadlinerequire(readline);constrlreadline.createInterface({input:process.stdin,output:process.stdout});letinput[];rl.on(line,(line){input.push(line.trim());});rl.on(close,(){letpathInputinput[0];letresponseInputinput[1];letpathspathInput.split(,);letresponseTimeresponseInput.split(,).map(Number);functionhanle(path,responseTime){letres[];letnpath.length;for(leti0;in;){lettotalTimeresponseTime[i];letji;// 获取相同路径起始和终止连续位置while(j1npath[j]path[j1]){totalTimeresponseTime[j1];j;}res.push([i,j-i1,Math.floor(totalTime/(j-i1))]);ij1;}returnres;}letreshanle(paths,responseTime);console.log(res.map(r${r[0]},${r[1]},${r[2]}).join( ));});Gopackagemainimport(bufiofmtosstrings)// 统计函数funchanle(path[]string,responseTime[]int)[][]int{res:[][]int{}n:len(path)fori:0;in;{totalTime:responseTime[i]j:i// 获取相同路径起始和终止连续位置forj1npath[j]path[j1]{totalTimeresponseTime[j1]j}resappend(res,[]int{i,j-i1,totalTime/(j-i1)})ij1}returnres}funcmain(){reader:bufio.NewReader(os.Stdin)pathInput,_:reader.ReadString(\n)responseInput,_:reader.ReadString(\n)// 去掉换行符pathInputstrings.TrimSpace(pathInput)responseInputstrings.TrimSpace(responseInput)paths:strings.Split(pathInput,,)tmp:strings.Split(responseInput,,)responseTime:make([]int,len(tmp))fori:0;ilen(tmp);i{fmt.Sscanf(tmp[i],%d,responseTime[i])}res:hanle(paths,responseTime)fori:0;ilen(res);i{fmt.Printf(%d,%d,%d,res[i][0],res[i][1],res[i][2])ifi!len(res)-1{fmt.Print( )}}}C语言#includestdio.h#includestring.h#includestdlib.h// 统计函数voidhanle(charpath[][100],intpathSize,intresponseTime[],intres[][3],int*resSize){intk0;for(inti0;ipathSize;){inttotalTimeresponseTime[i];intji;// 获取相同路径起始和终止连续位置while(j1pathSizestrcmp(path[j],path[j1])0){totalTimeresponseTime[j1];j;}res[k][0]i;res[k][1]j-i1;res[k][2]totalTime/(j-i1);k;ij1;}*resSizek;}intmain(){charpathInput[10300000],responseInput[10300000];fgets(pathInput,sizeof(pathInput),stdin);fgets(responseInput,sizeof(responseInput),stdin);// 去掉换行pathInput[strcspn(pathInput,\n)]0;responseInput[strcspn(responseInput,\n)]0;charpath[100005][100];intresponseTime[100005];intpathSize0;char*tokenstrtok(pathInput,,);while(token){strcpy(path[pathSize],token);tokenstrtok(NULL,,);}inti0;tokenstrtok(responseInput,,);while(token){responseTime[i]atoi(token);tokenstrtok(NULL,,);}intres[100005][3],resSize0;hanle(path,pathSize,responseTime,res,resSize);for(inti0;iresSize;i){printf(%d,%d,%d,res[i][0],res[i][1],res[i][2]);if(i!resSize-1)printf( );}return0;}