04 bytes beating pen questions kitty feature

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

(Programming questions) Xiao Ming is an algorithm engineer, but also a shovel feces officer. One day, he whim, I wanted to tap some of the movement information from the video kitty cat's. In order to extract the motion information he needed "feature cats" are extracted from each frame of the video. Cat is a feature of a two-dimensional vector <x, y>. If x_1 = x_2 and y_1 = y_2, maybe it is the same feature.
Therefore, if a consistent feature of Kitten, Kitten can be considered in motion. That is, if the characteristic <a, b> appears in the frame in duration, it will constitute a feature movement. For example, wherein <a, b> 2/3/4/7/8 first frame appears, then the characteristic features forming the two motion 2-3-4 and 7-8.
Now, given the characteristics of each frame, the number of features may be different. Xiao Ming expect to find the longest feature movement.

Reference Code

n = int(input())

while n > 0:
    m = int(input())
    res = 1
    d = {}
    for i in range(m):
        l = list(map(int , input().split()))
        k = l[0]
        tmp_d = {}
        for j in range(k):
            index = l[2 * j + 1] * 1000000000 + l[2 * j + 2]
            if index in d:
                tmp_d[index] = d[index] + 1
                res = max(res, tmp_d[index])
            else:
                tmp_d[index] = 1
        d = tmp_d
    print(res)
    n -= 1

c ++ version

map套set 存进去之后找连续的
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<bits/stdc++.h>
using namespace std;
map< pair<int,int>, set<int> > m1;
int main()
{
  //  freopen("in","r",stdin);
    int n,m;
    cin>>n;
    while(n--){
        cin>>m;
        int maxn=0,now=0;
        int k;
        int x,y;
        for(int i=0;i<m;i++){
            cin>>k;
            for(int j=0;j<k;j++){
                cin>>x>>y;
                m1[make_pair(x,y)].insert(i);
            }
        }
        for(map <pair<int,int>, set<int> >::iterator it=m1.begin();it!=m1.end();it++){
            int num=0;
            int sum=1;
            set<int>::iterator itt=it->second.begin();
            num=*itt;
            itt++;
            for(;itt!=it->second.end();itt++){
                if(*itt==num+1){
                    sum++;
                    num=*itt;
                    }
                    else 
                    {
                    	num=*itt;
                    	maxn=max(maxn,sum);
                    	sum=1;
                	}
            	}
            	if(itt==it->second.end()) maxn=max(maxn,sum);
        	}
        	cout<<max(maxn,now)<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44611644/article/details/95246572