poj 3349 Snowflake Snow Snowflakes

题意的大体意思是,给你n个雪花,如果有的雪花是一样的,就输出Twin snowflakes found.否则输出NO tow snowflakes are alike.但是数据的范围很大的啊,一般的暴力会超时的啊,我已开始搜了一下题解大家基本上都是用哈希过得啊、、可惜无奈菜鸟不会啊,后来看见牛牛同学的方法不错啊。就学习了啊、、大体就是给每个雪花的边排序,然后根据雪花的相似程度进行排序。。然后找再搜一遍如果有完全一样的就退出、、否则继续搜、、结题思路很灵活的啊、、值得学习啊、、赞一个啊!

Snowflake Snow Snowflakes
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 27349   Accepted: 7235

Description

You may have heard that no two snowflakes are alike. Your task is to write a program to determine whether this is really true. Your program will read information about a collection of snowflakes, and search for a pair that may be identical. Each snowflake has six arms. For each snowflake, your program will be provided with a measurement of the length of each of the six arms. Any pair of snowflakes which have the same lengths of corresponding arms should be flagged by your program as possibly identical.

Input

The first line of input will contain a single integer n, 0 < n ≤ 100000, the number of snowflakes to follow. This will be followed by n lines, each describing a snowflake. Each snowflake will be described by a line containing six integers (each integer is at least 0 and less than 10000000), the lengths of the arms of the snow ake. The lengths of the arms will be given in order around the snowflake (either clockwise or counterclockwise), but they may begin with any of the six arms. For example, the same snowflake could be described as 1 2 3 4 5 6 or 4 3 2 1 6 5.

Output

If all of the snowflakes are distinct, your program should print the message:
No two snowflakes are alike.
If there is a pair of possibly identical snow akes, your program should print the message:
Twin snowflakes found.

Sample Input

2
1 2 3 4 5 6
4 3 2 1 6 5

Sample Output

Twin snowflakes found.
#include 
#include 
#include 
struct node
{
    int f[6];
} snow[100200];
int cmp1(const void *a , const void *b)
{
    return *(int *)a - *(int *)b;
}
int cmp2(const void *a , const void *b)
{
    struct node *c = (struct node*)a;
    struct node *d = (struct node*)b;
    for(int i = 0; i < 6; i++)//根据每组数据的相似度进行排序;
    {
        if(c->f[i] != d->f[i])
            return c->f[i] - d->f[i];
    }
    return 0;
}
int main()
{
    int n, i, j, flat, a[7];
    while(~scanf("%d",&n))
    {
        for(i = 0; i < n; i++)
        {
            for(j = 0; j < 6; j++)
                scanf("%d",&a[j]);
            qsort(a , 6 , sizeof(a[0]) , cmp1);
            for(j = 0; j < 6; j++)
                snow[i].f[j] = a[j];
        }
        qsort(snow , n , sizeof(snow[0]) , cmp2);
        for(i = 0; i < n-1; i++)
        {
            flat = 1;
            for(j = 0; j < 6; j++)
                if(snow[i].f[j] != snow[i+1].f[j])
                {
                    flat = 0;
                    break;
                }
            if(flat)//只要有相同的就退出;
                break;
        }
        if(flat)
            printf("Twin snowflakes found.\n");
        else
            printf("No two snowflakes are alike.\n");
    }
    return 0;
}


你可能感兴趣的:(POJ刷题,查找法,POJ,ACM)