• 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;
    }
  • 相关阅读:
    【转】awk用法介绍
    【转】Shell执行MySql操作
    curl访问nagios中Host Status Details For All Host Groups页面的方法
    【转】命令行浏览器 curl 命令详解,Linux中访问url地址
    【转】DELL R710服务器可以安装的VMWare ESX Server 4.1 全套下载带注册码
    【转】一些常用的Vi命令,可帮助脱离鼠标
    乐观处世,诚实做人,不骄不躁,积极进取; 勇于创新,踏实实现,谨慎规划,付诸实践; 事在人为
    【转】Linux方向职业分析
    【转】[Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!
    【转】Nagios安装部署与Cacti整合文档超精细版本
  • 原文地址:https://www.cnblogs.com/chenhuan001/p/3079010.html
Copyright © 2020-2023  润新知