题目:https://www.nowcoder.com/ta/2019test?page=10
题解:使用 map<int, pair<int, int>> mp; // 特征,特征起点帧的序号,特征计数
遍历每个帧,加入新特征或者更新已存在特征的帧起点,特征计数
1、如果该特征不存在,则添加该特征到map
2、如果该特征存在
(1)如果该特征是连续的,通过判断(当前帧号-特征起点帧号 == 特征计数),计数+1;
(2)如果该特征不是连续的,更新特征起点为当前帧号,计数=1;
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
for(int i=0; i<n; i++)
{
int m;
cin >> m;
map<int, pair<int, int>> mp; //特征,起始点,计数(判断是否连续)
int maxlen = 1;
for(int j=0; j<m; j++) //帧数
{
int k;
cin >> k; //本帧特征数
for(int i=0; i<k; i++) //2
{
int x, y;
cin >> x >> y; //特征由 x*10+y 表示
if(mp.find(x*10+y) == mp.end()) //原本无该特征,添加该特征
{
mp[x*10+y].first = j; //该特征,起点帧
mp[x*10+y].second = 1; //该特征,计数
}
else //原本有该特征,更新该特征
{
pair<int, int> &p = mp[x*10+y];
if(j-p.first == p.second) //该特征是连续的
{
p.second += 1; //起始位置不变,计数+1
maxlen = max(maxlen, p.second); //更新最长长度
}
else //该特征不是连续的
{
p.first = j; //起始位置更新为当前位置
p.second = 1; //计数更新为1
}
}
}
}
cout << maxlen;
}
return 0;
}
京公网安备 11010502036488号