Nine degrees OJ 1156 who is your potential friend (hash + count) _ Count

Source: Internet
Author: User

Original title address: http://ac.jobdu.com/problem.php?pid=1156

Topic Description:

"Like-Minded"--this is the vocabulary we like to use when describing friends. Two people who are friends usually mean they have a lot of common interests. As an otaku, however, you find that you don't have much chance of understanding each other. Luckily, you accidentally got a book loan record from the Peking University Library, so you Midnight oil tonight to stay up all night programming to find potential friends.
First of all, you have to borrow records to do some sorting, the N readers sequentially numbered 1,2,..., N, the M book sequentially numbered 1,2,..., m. At the same time, according to the "like-minded" principle, and the person you like to read the same book, is your potential friend. Your current assignment is to figure out how many potential friends each person has in this loan record. Input:

Each case first line two integers n,m,2 <= N, m<= 200. Next there are N rows, and the first (i = 1,2,..., n) row has a number that represents the number of the reader i-1 favorite book P (1<=p<=m) Output:

Each case consists of n rows, one number per line, and the number of rows I have for readers I have several potential friends. If I and anyone have no common book, then output "Beiju" (ie tragedy, ^ ^) sample input:

4  5
2
3
2
1
Sample output:
1
beiju
1
beiju
Source:

2011 Peking University Computer Research Life test real problem.


This is strictly not the case of a typical type of problem, should be considered as a programming skills. In the book called "Hash Application", it seems that the hash function that I understand (that is, the hash function makes the access process to a sequence of data more efficient, by hashing the function, the data element will be positioned faster, or the hash function in the message digest) is different, But its main idea is to match the storage location to the data itself. Previously encountered this kind of problem, roughly all by the array subscript to correspond with the data.

In this case, the number p of the book corresponds to the subscript of the Bookcommon array, and the value of Bookcommon[i] is the number of people who like the book. This idea is too natural, reader[i] does not represent the first reader.

The AC code is as follows:

#include <iostream>
#include <algorithm>
#define MAXN 201
#define MAXM 201
using namespace Std;

int main ()
{
    int n, m, P;
    int READER[MAXN];  Record which book each person likes while
    (Cin >> n >> m)
    {
        int bookcommon[maxm] = {0};//Record the number of people who like a book for
        (int i=0; i<n; ++i)
        {
            cin >> P;
            Reader[i] = P;
            bookcommon[p]++;
        }
        for (int i=0; i<n; ++i)
        {
            int anyfriend = Bookcommon[reader[i]]-1;////Remove the number of people you are in
            if (anyfriend!= 0) 
  cout << anyfriend << Endl;
            else cout << "Beiju" << Endl
        }
    }
    return 0;
}

Memory footprint: 1520Kb time consuming: 20ms


Similar topics include:

Nine degrees OJ 1018--statistics of the number of the same results, with an array of subscript to indicate student scores (0-100), the value represents the number of the same score, for the input I output A[I]

Nine-degree OJ 1431--sort, with an array subscript to indicate the number I, the value indicates whether the number, from the largest subscript start down the count of the largest number of M (input varies)

Nine degrees OJ 1088--the remainder of the tree, with an array subscript representing the position of I tree, the value indicates that the tree has not been removed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.