nyoj130 相同的雪花

 

相同的雪花

时间限制: 1000 ms  |  内存限制:65535 KB
难度: 4
 
<dl class="problem&#45;display"> <dt> 描述 </dt> <dd> 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. </dd> </dl>
 
<dl class="others"> <dt> 输入 </dt> <dd> The first line of the input will contain a single interger T(0<T<10),the number of the test cases.
The first line of every test case 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. </dd> <dt> 输出 </dt> <dd> For each test case,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. </dd> <dt> 样例输入 </dt> <dd>
1
2
1 2 3 4 5 6
4 3 2 1 6 5
</dd> <dt> 样例输出 </dt> <dd>
Twin snowflakes found.
</dd> </dl>

 解题思路:用到哈希的知识,通过观察可以发现,如果是两片相同的雪花,那么他的和一定是相同的,那就把和当做它的地址来存储,然后如果和相同就都连接在相应的和的后面,采用的是链地址法,然后再从和相同的里面来寻找相同的雪花,就大大的减少了比较次数。

代码:

 

#include <iostream>
#include <cstdio>

using namespace std;

typedef struct snow{
    int a[6];
    snow *next;
};
snow *s[100000];

int fin(int b[],int sum){
    snow *p;
    p=s[sum]->next;
    while(p){
        for(int i=0;i<6;i++){
            int j;
            if(b[i]==p->a[0]){
                for(j=1;j<6;j++){
                    if(b[i+j]%6!=p->a[j]){
                        break;
                    }
                }
                if(j==6){
                    return 1;
                }
                for(j=1;j<6;j++){
                    if(b[((i-j%6+6)%6)]!=p->a[j]){
                        break;
                    }
                }
                if(j==6){
                    return 1;
                }
            }
        }
        p=p->next;
    }
    p=new snow;
    for(int i=0;i<6;i++){
        p->a[i]=b[i];
    }
    p->next=s[sum]->next;
    s[sum]->next=p;
    return 0;
}

int main()
{
    int n;
    int t;
    int b[6];
    int sum;
    int flag;
    scanf("%d",&n);
    for(int i=0;i<100000;i++){
        s[i]=new snow;
        s[i]->next=NULL;
    }
    while(n--){
        scanf("%d",&t);
        flag=0;
        for(int i=0;i<t;i++){
            sum=0;
            for(int j=0;j<6;j++){
                scanf("%d",&b[j]);
                sum+=b[j];
            }
            sum%=100000;
            if(!flag){
                if(fin(b,sum)){
                    flag=1;
                }
            }
        }
        if(flag){
            printf("Twin snowflakes found.\n");
        }else{
            printf("No two snowflakes are alike.\n");
        }
    }

    return 0;
}

 

全部评论

相关推荐

03-06 18:20
门头沟学院 Java
点赞 评论 收藏
分享
刚刷到字节跳动官方发的消息,确实被这波阵仗吓了一跳。在大家还在纠结今年行情是不是又“寒冬”的时候,字节直接甩出了史上规模最大的转正实习计划——ByteIntern。咱们直接看几个最硬的数,别被花里胡哨的宣传词绕晕了。首先是“量大”。全球招7000多人是什么概念?这几乎是把很多中型互联网公司的总人数都给招进来了。最关键的是,这次的资源分配非常精准:研发岗给了4800多个Offer,占比直接超过六成。说白了,字节今年还是要死磕技术,尤其是产品和AI领域,这对于咱们写代码的同学来说,绝对是今年最厚的一块肥肉。其次是大家最关心的“转正率”。官方直接白纸黑字写了:整体转正率超过50%。这意味着只要你进去了,不划水、正常干,每两个人里就有一个能直接拿校招Offer。对于2027届(2026年9月到2027年8月毕业)的同学来说,这不仅是实习,这简直就是通往大厂的快捷通道。不过,我也得泼盆冷水。坑位多,不代表门槛低。字节的实习面试出了名的爱考算法和工程实操,尤其是今年重点倾斜AI方向,如果你简历里有和AI相关的项目,优势还是有的。而且,转正率50%也意味着剩下那50%的人是陪跑的,进去之后的考核压力肯定不小。一句话总结:&nbsp;27届的兄弟们,别犹豫了。今年字节这是铁了心要抢提前批的人才,现在投递就是占坑。与其等到明年秋招去千军万马挤独木桥,不如现在进去先占个工位,把转正名额攥在手里。
喵_coding:别逗了 50%转正率 仔细想想 就是转正与不转正
字节7000实习来了,你...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务