1063 Set Similarity java

版权声明:转载表明出处即可 @_@ https://blog.csdn.net/Cai_Nia/article/details/90693959

Given two sets of integers, the similarity of the sets is defined to be N​c​​/N​t​​×100%, where N​c​​ is the number of distinct common numbers shared by the two sets, and N​t​​ is the total number of distinct numbers in the two sets. Your job is to calculate the similarity of any given pair of sets.

Input Specification:

Each input file contains one test case. Each case first gives a positive integer N (≤50) which is the total number of sets. Then Nlines follow, each gives a set with a positive M (≤10​4​​) and followed by M integers in the range [0,10​9​​]. After the input of sets, a positive integer K (≤2000) is given, followed by K lines of queries. Each query gives a pair of set numbers (the sets are numbered from 1 to N). All the numbers in a line are separated by a space.

Output Specification:

For each query, print in one line the similarity of the sets, in the percentage form accurate up to 1 decimal place.

Sample Input:

3
3 99 87 101
4 87 101 5 87
7 99 101 18 5 135 18 99
2
1 2
1 3

Sample Output:

50.0%
33.3%
import java.util.HashSet;
import java.util.Scanner;

public class Main{
    /**
     * 在牛客网上了pat提交正确,
     * */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        HashSet<Integer> setS[] = new HashSet[n + 1];
        for (int i = 1; i <= n; ++i) {
            int k = scanner.nextInt();
            HashSet<Integer> eachSet = new HashSet<>();
            for (int j = 0; j < k; ++j) {
                int eachElement = scanner.nextInt();
                eachSet.add(eachElement);
            }
            setS[i] = eachSet;
        }
        int m = scanner.nextInt();
        for (int i = 0; i < m; ++i) {
            int indexA = scanner.nextInt(), indexB = scanner.nextInt();
            int lenA = setS[indexA].size(), lenB = setS[indexB].size(), commentLen = 0;
            int minLen = lenA <= lenB ? lenA : lenB;
            if (minLen != lenA) {
                int tempIndex = indexA;
                indexA = indexB;
                indexB = tempIndex;
            }
            // 遍历较短的集合,不然会有一组数据超时
            for (Integer each : setS[indexA]) {
                if (setS[indexB].contains(each)) {
                    ++commentLen;
                }
            }
            System.out.printf("%.1f%c\n", commentLen * 1.0 / (lenA + lenB - commentLen) * 100, '%');
        }

    }
}

 

展开阅读全文

Similarity

08-29

When we were children, we were always asked to do the classification homework. For example, we were given words Tiger, Panda, Potato, Dog, Tomato, Pea, Apple, Pear, Orange, Mango and we were required to classify these words into three groups. As you know, the correct classification was Tiger, Panda, Dog, Potato, Tomato, Pea and Apple, Pear, Orange, Mango. We can represent this classification with a mapping sequence A,A,B,A,B,B,C,C,C,C, and it means Tiger, Panda, Dog belong to group A, Potato, Tomato, Pea are in the group B, and Apple, Pear, Orange, Mango are in the group C.nnBut the LABEL of group doesn't make sense and the LABEL is just used to indicate different groups. So the representations P,P,O,P,O,O,Q,Q,Q,Q and E,E,F,E,F,F,W,W,W,W are equivalent to the original mapping sequence. However, the representations A,A,A,A,B,B,C,C,C,C and D,D,D,D,D,D,G,G,G,G are not equivalent.nnSimilaritynThe pupils in class submit their mapping sequences and the teacher should read and grade the homework. The teacher grades the homework by calculating the maximum similarity between pupils' mapping sequences and the answer sequence. The definition of similarity is as follow.nnSimilarity(S, T) = sum(Si == Ti) / LnnL = Length(S) = Length(T), i = 1, 2,... L, where sum(Si == Ti) indicates the total number of equal labels in corresponding positions.nnThe maximum similarity means the maximum similarities between S and all equivalent sequences of T, where S is the answer and fixed.nnNow given all sequences submitted by pupils and the answer sequence, you should calculate the sequences' maximum similarity.nnInputnnThe input contains multiple test cases. The first line is the total number of cases T (T < 15). The following are T blocks. Each block indicates a case. A case begins with three numbers n (0 < n < 10000), k (0 < k < 27), m (0 < m < 30), which are the total number of objects, groups, and students in the class. The next line consists of n labels and each label is in the range [A...Z]. You can assume that the number of different labels in the sequence is exactly k. This sequence represents the answer. The following are m lines, each line contains n labels and each label also is in the range [A...Z]. These m lines represent the m pupils' answer sequences. You can assume that the number of different labels in each sequence doesn't exceed k.nnOutputnnFor each test case, output m lines, each line is a floating number (Round to 4 digits after the decimal point). You should output the m answers in the order of the sequences appearance.nnSample Inputnn2 n10 3 3nA A B A B B C C C CnF F E F E E D D D DnX X X Y Y Y Y Z Z ZnS T R S T R S T R Sn3 2 2nA B AnC D CnF F EnSample Outputnn1.0000n0.7000n0.5000n1.0000n0.6667 问答

Similarity of necklaces

02-05

DescriptionnnThe story is based on a real dream of mine. The description of rules is exactly what I dreamt. Do not start to code before you have understood it well. nnThe little cat is preparing a bead necklace for his girlfriend - a DIY one to express his affection. N (4 <= N <= 400) different kinds of beads (wooden, plastic, glass, pearl, DIAMOND!?, etc) have been kept in his huge casket, which are the only source of heads to make his necklace. nnBy the impulse of curiosity, the little cat made two strings of beads unthinkingly, one for him and one for his girlfriend – a pair of "lover-necklaces". Will his girlfriend like it? By the market research, how they look like depends on the similarity factor of two necklaces. A necklace can be considered as a linked list of M beads. nnThe similarity of two beads forms an N * N table. For example: nn nnThe similarity factor of two necklaces can be determined by the sum of M corresponding pair of beads. For example: nnNecklace1: WPPWW nNecklace2: DPDDW nSimilarly factor = Table[W,D] + Table[P,P] + Table[P,D] + Table[W,D] + Table[W,W] = -7 + 1 + 1 + (-7) + 6 = -6 nnSince we do not much care about what the necklace exactly is, (what is more, the little cat wants to keep it secret until his girlfriend's birthday) we use another N * N table to record number of existences of different pairs: nn nnYou are given the table Pairs, but to determine the table Table. The purpose is to make the similarity factor of two necklaces as large as possible. There's one more restriction holds for Table, Low[i, j] <= Table[i, j] <= Up[i, j] (an upper bound and an lower bound given to you). Obviously Low[i, j] = Low[j, i] and Up[i, j] = Up[j, i]. nnBackground in my dream: the little cat has special magic. At one time, he can turn all the pearls to diamonds, and all the diamonds to pearls. More precise speaking, he can swap two different kinds of beads at one time. That is why N * N Pairs is fixed, but we can modify Table ourselves! nInputnnThe input contains a number of test cases. Each of the following blocks denotes a single test case. nA test case starts by an integer N (4 <= N <= 400) and N lines followed. The i-th line followed contains 3 * (N – i + 1) integers, in which the j-th triple denotes Pairs[i, j + i - 1], Low[i, j + i - 1] and Up[i, j + i - 1]. nnRestrictions: -10000 <= Low[i, j] < Up[i, j] <= 10000, -100000 <= Pair[i, j] <= 100000. From the input given, you may assume that there is always a solution. nOutputnnFor each test case, output a single line with a single number, which is the largest similarity that you can generate.nSample Inputnn4n7 1 10 0 -10 10 2 -10 10 0 -10 10n 0 1 10 0 -10 10 0 -10 10n 0 1 10 0 -10 10n 0 1 10nn4n0 1 10 2 -10 10 2 -10 10 2 -10 10n 0 1 10 2 -10 10 2 -10 10n 0 1 10 2 -10 10n 0 1 10nSample Outputnn90n-4 问答

没有更多推荐了,返回首页