zoukankan      html  css  js  c++  java
  • poj 3349(hash判重)

    挺有意思的题。

    思路不难, 用hash判重, 但是要坑人的是一开始怎么将每串序列按照同一规律排列。 

    比如

    2 1 2 1 1 2  

    1 1 2 2 1 2

    怎么转化为 1 1 2 2 1 2 ,也就是转化为字典序最小, 最后没有办法只有枚举, 3000+ms 险过.

    Snowflake Snow Snowflakes
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 26254   Accepted: 6900

    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.

    Source

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    using namespace std;
    #define N 1010000
    #define MOD 1000007
    #define INF 10000001
    typedef __int64 LL;
    
    struct node
    {
        int next;
        int g[6];
    }edge[N];
    
    int cnt,pre[N];
    int flag1;
    
    
    void hash(LL key,int k[6])
    {
        for(int p=pre[key];p!=-1;p=edge[p].next)
        {
            int ff=0;
            for(int i=0;i<6;i++)
                if(k[i]!=edge[p].g[i])
                {
                    ff=1;
                    break;
                }
            if(ff==0)
            {
                flag1=1;
                return ;
            }
        }
        for(int i=0;i<6;i++)
            edge[cnt].g[i]=k[i];
        edge[cnt].next=pre[key];
        pre[key]=cnt++;
    }
    
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            int k[6],tk[6],mk[6];
            cnt=0;
            memset(pre,-1,sizeof(pre));
            flag1=0;
            for(int i=0;i<n;i++)
            {
                for(int i=0;i<6;i++)
                mk[i]=INF;
                for(int j=0;j<6;j++)
                {
                    scanf("%d",&k[j]);
                }
                if(flag1==1) continue;
                /*int flag=1;
                int t1=s,t2=s;
                for(int j=0;j<5;j++)
                {
                    t1++;
                    if(t1>=6) t1-=6;
                    t2--;
                    if(t2<0) t2+=6;
                    if(k[t1]>k[t2])
                    {
                        flag=-1; // 说明是逆向的
                        break;
                    }
                    if(k[t1]<k[t2])
                    {
                        flag=1;
                        break;
                    }
                }
                for(int j=0;j<6;j++)
                {
                    tk[j]=k[s];
                    s += flag;
                    if(s>=6) s-=6;
                    if(s<0) s+=6;
                }*/
                for(int j=0;j<6;j++)
                {
                    for(int i1=0;i1<6;i1++)
                        tk[i1]=k[(i1+j)%6];
                    int f=0;
                    for(int i1=0;i1<6;i1++)
                    {
                        if(mk[i1]<tk[i1])
                        {
                            break;
                        }
                        if(mk[i1]>tk[i1])
                        {
                            f=1;
                            break;
                        }
                    }
                    if(f==1)
                    {
                        for(int i1=0;i1<6;i1++)
                            mk[i1]=tk[i1];
                    }
                }
                for(int j=0;j<6;j++)
                {
                    for(int i1=0;i1<6;i1++)
                        tk[i1]=k[(j-i1+6)%6];
                    int f=0;
                    for(int i1=0;i1<6;i1++)
                    {
                        if(mk[i1]<tk[i1])
                        {
                            break;
                        }
                        if(mk[i1]>tk[i1])
                        {
                            f=1;
                            break;
                        }
                    }
                    if(f==1)
                    {
                        for(int i1=0;i1<6;i1++)
                            mk[i1]=tk[i1];
                    }
                }
                LL tmp=0;
                for(int j=0;j<6;j++)
                {
                    tmp=(tmp*100000007+mk[j])%MOD;
                }
                hash(tmp,mk);
            }
            if(flag1==0) printf("No two snowflakes are alike.\n");
            else printf("Twin snowflakes found.\n");
        }
        return 0;
    }
  • 相关阅读:
    学习制作操作系统 0
    阅读《C陷阱与缺陷》的知识增量
    CSS 优先级和特指度
    openCV2马拉松第19圈——Harris角点检測(自己实现)
    Cacti监控mysql数据库server实现过程
    ledisdb:支持类redis接口的嵌入式nosql
    03005_SQL查询语句
    通过smtp直接发送邮件
    XML 解析默认去掉命名空间和注释
    C# /VB.NET 创建PDF项目符号列表和多级编号列表
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3079010.html
Copyright © 2011-2022 走看看