Today's headlines 2018 autumn recruitment programming questions

Today's headlines 2018 autumn recruitment Android direction programming questions


1. Bracelet

As a bracelet artist, a gold master has ordered you a bracelet containing n variegated beads - each bead is either colorless or painted in several colors. In order to make the color of the bracelet look less monotonous, the gold master requires that any color (excluding colorless) on the bracelet appears at most once in any continuous m beads (note: the bracelet is a ring) . There are a total of c colors on the bracelet. Now tell you in clockwise order what colors are contained in each bead on a bracelet with n beads. Please judge that there are several colors on the bracelet that do not meet the requirements. That is, ask how many colors appear at least twice in any consecutive m strings of beads.
Input description:
Enter n, m, c on the first line, separated by spaces. (1<=n <= 10000, 1<=m<=1000, 1<=c<=50), the next n lines, the first number num (0<=num<=c) in each line represents the first The i bead has several colors, followed by num numbers, each number x means that the i-th bead contains the x-th color.
Output description:
A non-negative integer, indicating that there are several colors on the bracelet that do not meet the requirements.

Example:

Input:
5 2 3
3 1 2 3
0
2 2 3
1 2
1 3
Output:
2
Explanation:
The first color appears at: 1, meets the requirements
The second color appears at: 1, 3, 4, 3 and 4 Adjacent, not matching The
third color appears at: 1, 3, 5, 5 and 1 are adjacent and therefore not matching.
So there are 2 colors that do not meet the requirements.

Parse:

Use the two-dimensional array colors[][] to record which beads appear on each color.
First, iterate over each bead and record the number of beads that each color appears on in the colors array.
For each color in the colors,
determine whether the beads in it are adjacent to m, such as m=3, 1 and 3 are considered adjacent, and 1 and 4 are not adjacent.
It should be noted here that the bracelet is annular, and special cases need to be handled.
See the code for details.

C++ code implementation:

#include <iostream>
#include <vector>
using namespace std;

int findColors(int n, int m, int c,const vector<vector<int>>& colors)
{
    if(m==1)
        return 0;
    vector<vector<int>> colorIndex(c);
    int count = 0, i=0,j=0;
    for(; i<n; i++){
        count = colors[i].size();
        for(j=0; j<count; j++){
            colorIndex[colors[i][j]-1].push_back(i+1);
        }
    }

    int result=0;
    for(i=0; i<c; i++){
       count = colorIndex[i].size();
       if(count<=1)
            continue;
       for(j=0; j<count-1; j++){
            //查找是否存在m相邻的珠子
          auto it = lower_bound(colorIndex[i].begin(),colorIndex[i].end(),colorIndex[i][j]+1);
          if(it!=colorIndex[i].end() && *it-colorIndex[i][j] < m){
             result++;
             break;
          }
       }
       if(j==count-1 && (colorIndex[i][j]+1)%n == colorIndex[i][0]){  //手串是环形的,特殊情况
            result++;
       }
    }
    return result;
}

int main()
{
    int n,m,c,t,k;
    cin>>n>>m>>c;
    vector<vector<int>> colors;
    for(int i=0; i<n; i++){
        cin>>t;
        vector<int> color;
        for(int j=0; j<t; j++){
            cin>>k;
            color.push_back(k);
        }
        colors.push_back(color);
    }
    cout<<findColors(n,m,c,colors);
}

2. Article preferences

Given the preference of n headline users for a certain type of article, find the number of users whose preference is q for a certain type of article. Enter the number of q lines, and the number of each line is l,r,k, indicating that the user's preference for a certain type of article is k within the range of [l,r]. Each line outputs the corresponding number of people.

Example:

Input:
5
1 2 3 3 5 (indicates the preference of 5 users for a certain type of article)
3
1 3 2
2 4 3
3 5 5
Output:
1 (Among users 1 to 3, only user 2 has a preference of 2 )
2 (among users 2 to 4, users 3 and 4 have a preference degree of 3)
1 (among users 3 to 5, only user 5 has a preference degree of 5)

c++ code implementation:

void query(int n,int *users,int q,int questions[][3])
{
    int cnt;
    for(int i=0; i<q; i++){
        cnt = 0;
        for(int j=questions[i][0]; j<=questions[i][1]; j++){
            if(users[j-1]==questions[i][2])
            cnt++;
        }
        cout<<cnt<<endl;
    }
}

int main()
{
    int n;
    cin>>n;
    int users[n];
    for(int i=0; i<n; i++)
        cin>>users[i];
    int q;
    cin>>q;
    int questions[q][3];
    for(int i=0; i<q; i++)
        cin>>questions[i][0]>>questions[i][1]>>questions[i][2];
    query(n,users,q,questions);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326306545&siteId=291194637