poj 3349 Snowflake Snow Snowflakes(hash表)

SnowflakeSnowSnowflakes
点击打开题目链接
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 30772 Accepted: 8120

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 byn 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

CCC 2007


题目大意:,给你n个雪花的边长,边长是按顺时针或者逆时针的顺序给出的,并且可以从这六个数中的任何一个数开始,例如:1,2,3,4,5,6,顺时针可以为,4,3,2,1,6,5、如果在这n个雪花中存在两个雪花的边长相等,则输出“Twin snowflakes found.”否则输出“ No two snowflakes are alike.”.
注:题目中说的两朵雪花的边长相等是指任意方向只要存在一个顺序使得相等即可;例如,1、2、3、1、3、2和1、3、2、1、2、3均是相同的;
故输出“Twin snowflakes found.”

1.题目中数据量大,而且要多次查询,所以用hash来提高查询速度;将雪花的六个边长的和作为key值,若两个雪花相同,则key值必然相同。所以,对于key值不同
的雪花便可不必判断是否相等。
2.用拉链法解决hash冲突,找到冲突的key值所在的hash表,遍历其链表,判断雪花是否相同。
3.判断雪花相同的方法:
   在hash表中找到与要判断雪花长度ssnow[0]相同的位置记录下来(相同的位置可能不止一处)对于每个位置逆时针顺时针判断;
    顺时针时,循环变量k初值为pos[pn]+1,即hash中与雪花相同位置的下一个位置;只要判断下标为k%6的长度是否与相同
    逆时针时,循环变量仍为pos[pn]+1,此时要判断下标为[k%6<0?k%6+6:k%6]的与ssnow[j]相不相同即可


╮(╯▽╰)╭,因为标志变量的初值赋在循环外了,因此贡献了好多WA ,教训%>_<%
代码:
#include<iostream>
#include <stdio.h>
#include <string.h>
#define prime 999983
using namespace std;
struct SNOW
{
    SNOW()//初始化
    {
        memset(snow,0,sizeof(snow));
        next=NULL;
    }
    bool operator!=(int x)//重载不等号
    {
        int i;
        for(i=0; i<6; i++)
        {
            if(this->snow[i]!=x)
                return true;
        }
        return false;
    }
    bool operator==(int x)//重载等号
    {
        int i;
        for(i=0; i<6; i++)
        {
            if(this->snow[i]!=x)
                return false;
        }
        return true;
    }
    int snow[6];
    SNOW *next;
} hash[prime+1];
int ssnow[6];
void Hash(int s[],int key)//hash表构建
{

    if(hash[key]!=0)//解决hash冲突
    {
        SNOW *SK=new SNOW;
        SNOW *p=&hash[key];
        while(p->next!=NULL)
            p=p->next;
        p->next=SK;
        SK->next=NULL;
        SK->snow[0]=s[0];
        SK->snow[1]=s[1];
        SK->snow[2]=s[2];
        SK->snow[3]=s[3];
        SK->snow[4]=s[4];
        SK->snow[5]=s[5];
    }
    else
    {
        hash[key].snow[0]=s[0];
        hash[key].snow[1]=s[1];
        hash[key].snow[2]=s[2];
        hash[key].snow[3]=s[3];
        hash[key].snow[4]=s[4];
        hash[key].snow[5]=s[5];
    }
}
bool judge(int key,int ssnow[])//判断是否相同
{
    SNOW *curr_hash=&hash[key];//获取链表的头结点
    int i,j,k,num,pos[6],pn;
    bool unlike;
    while(curr_hash)//遍历链表
    {
        for(i=0,num=0; i<6; i++)
        {
            if(curr_hash->snow[i]==ssnow[0])//找到与ssnow[0]相同的位置
            {
                pos[num++]=i;
            }
        }
        for(pn=0; pn<num; pn++)//从与ssnow[0]相同的位置开始循环查找
        {
            unlike=false;
            for(k=pos[pn]+1,j=1; j<6; k++,j++)//顺时针判断
            {
                if(curr_hash->snow[k%6]!=ssnow[j])//如果找到不同的则将不是的变量赋为真
                {
                    unlike=true;
                    break;
                }
            }
            if(!unlike)如果unlike变量为假,则说明找到相同的,函数返回真
            {
                return true;
            }
            unlike=false;
            for(k=pos[pn]-1,j=1; j<6; k--,j++)//逆时针判断
            {
                if(curr_hash->snow[k%6<0?k%6+6:k%6]!=ssnow[j])//判断相应下标是否相等
                {
                    unlike=true;
                    break;
                }
            }
            if(!unlike)
                return true;
        }
        curr_hash=curr_hash->next;
    }
    return false;
}
int main()
{
    int n;
    int key;
    bool flag;
    scanf("%d",&n);
    {
        flag=true;
        while(n--)
        {
            scanf("%d%d%d%d%d%d",ssnow,ssnow+1,ssnow+2,ssnow+3,ssnow+4,ssnow+5);
            key=(ssnow[0]%prime+ssnow[1]%prime+ssnow[2]%prime+ssnow[3]%prime+ssnow[4]%prime+ssnow[5]%prime)%prime;//
            if((hash[key]!=0)&&flag)
            {
                if(judge(key,ssnow))
                {
                    flag=false;
                }
                else
                    Hash(ssnow,key);
            }
            else if((hash[key]==0)&&flag)
            {
                Hash(ssnow,key);
            }
        }
        if(!flag)
            printf("Twin snowflakes found.\n");
        else
            printf("No two snowflakes are alike.\n");
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值