HASH的应用,思路如下: 两组相同的数字其和必然相等,故使用“每组数字的和”进行hash, 相当于根据和进行分类,只有分在同一类的才有可能相同,从而减少比较次数。 源代码如下: /*Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 16501 Accepted: 4209 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 "stdlib.h" #include "stdio.h" #include "math.h" #define SNOWFLAKE_HASH_LEN 200001 #define SNOWFLAKE_ARM_NUMBER 6 #define SNOWFLAKE_NUMBER 100001 #define NO_TWIN_SNOWFLAKES_FOUND 0 #define TWIN_SNOWFLAKES_FOUND 1 typedef unsigned int UINT32; typedef struct _SNOWFLAKE_NODE_ST_ { UINT32 auiArmLenght[SNOWFLAKE_ARM_NUMBER]; UINT32 uiNextNodeIndex; }SNOWFLAKE_NODE_ST; typedef struct _SNOWFLAKE_ST_ { UINT32 uiSnowFlakeNum; SNOWFLAKE_NODE_ST astSnowFlake[SNOWFLAKE_NUMBER+1]; }SNOWFLAKE_ST; SNOWFLAKE_ST gastSnowflake; UINT32 gauiSnowflakeHash[SNOWFLAKE_HASH_LEN]; /************************************************************************/ /* */ /* */ /* */ /* */ /************************************************************************/ void SnowflakeInput(void) { UINT32 uiLoop; scanf("%d",&gastSnowflake.uiSnowFlakeNum ); for(uiLoop = 1; uiLoop <= gastSnowflake.uiSnowFlakeNum; uiLoop++) { scanf("%d %d %d %d %d %d",&gastSnowflake.astSnowFlake[uiLoop].auiArmLenght[0],/ &gastSnowflake.astSnowFlake[uiLoop].auiArmLenght[1],/ &gastSnowflake.astSnowFlake[uiLoop].auiArmLenght[2],/ &gastSnowflake.astSnowFlake[uiLoop].auiArmLenght[3],/ &gastSnowflake.astSnowFlake[uiLoop].auiArmLenght[4],/ &gastSnowflake.astSnowFlake[uiLoop].auiArmLenght[5]); gastSnowflake.astSnowFlake[uiLoop].uiNextNodeIndex = 0; } return; } int InitSnowflakeHashAndSearch() { UINT32 uiLoopSnowflake; UINT32 uiHashIndex; UINT32 uiNextNodeIndex; UINT32 uiLoop_1; UINT32 uiLoop_2; UINT32 uiLoop_3; UINT32 uiNext_1; UINT32 uiNext_2; UINT32 uiAverage; memset(gauiSnowflakeHash,0,SNOWFLAKE_HASH_LEN*sizeof(UINT32)); for(uiLoopSnowflake = 1; uiLoopSnowflake <= gastSnowflake.uiSnowFlakeNum; uiLoopSnowflake++) { uiHashIndex = gastSnowflake.astSnowFlake[uiLoopSnowflake].auiArmLenght[0]+/ gastSnowflake.astSnowFlake[uiLoopSnowflake].auiArmLenght[1]+/ gastSnowflake.astSnowFlake[uiLoopSnowflake].auiArmLenght[2]+/ gastSnowflake.astSnowFlake[uiLoopSnowflake].auiArmLenght[3]+/ gastSnowflake.astSnowFlake[uiLoopSnowflake].auiArmLenght[4]+/ gastSnowflake.astSnowFlake[uiLoopSnowflake].auiArmLenght[5]; if (SNOWFLAKE_HASH_LEN <= uiHashIndex) { uiHashIndex = uiHashIndex % SNOWFLAKE_HASH_LEN; } if(0 != gauiSnowflakeHash[uiHashIndex]) { uiNextNodeIndex = gauiSnowflakeHash[uiHashIndex]; while (1) { /************************************************************************/ /* judge if Twin snowflakes found */ /************************************************************************/ for (uiLoop_1 = 0; uiLoop_1 < SNOWFLAKE_ARM_NUMBER; uiLoop_1++) { for (uiLoop_2 = 0; uiLoop_2 < SNOWFLAKE_ARM_NUMBER; uiLoop_2++) { if(gastSnowflake.astSnowFlake[uiLoopSnowflake].auiArmLenght[uiLoop_1] == gastSnowflake.astSnowFlake[uiNextNodeIndex].auiArmLenght[uiLoop_2]) { /**/ uiNext_1 = (uiLoop_1+1)%SNOWFLAKE_ARM_NUMBER; uiNext_2 = (uiLoop_2+1)%SNOWFLAKE_ARM_NUMBER; for (uiLoop_3 = 0; uiLoop_3 < SNOWFLAKE_ARM_NUMBER-1; uiLoop_3++) { if(gastSnowflake.astSnowFlake[uiLoopSnowflake].auiArmLenght[uiNext_1] != gastSnowflake.astSnowFlake[uiNextNodeIndex].auiArmLenght[uiNext_2]) { break; } uiNext_1 = (uiNext_1+1)%SNOWFLAKE_ARM_NUMBER; uiNext_2 = (uiNext_2+1)%SNOWFLAKE_ARM_NUMBER; } if (uiLoop_3 >= SNOWFLAKE_ARM_NUMBER-1) { printf("Twin snowflakes found./n"); return TWIN_SNOWFLAKES_FOUND; } /************************************************************************/ /* */ /************************************************************************/ uiNext_1 = (uiLoop_1+1)%SNOWFLAKE_ARM_NUMBER; uiNext_2 = (uiLoop_2-1)%SNOWFLAKE_ARM_NUMBER; for (uiLoop_3 = 0; uiLoop_3 < SNOWFLAKE_ARM_NUMBER-1; uiLoop_3++) { if(gastSnowflake.astSnowFlake[uiLoopSnowflake].auiArmLenght[uiNext_1] != gastSnowflake.astSnowFlake[uiNextNodeIndex].auiArmLenght[uiNext_2]) { break; } uiNext_1 = (uiNext_1+1)%SNOWFLAKE_ARM_NUMBER; uiNext_2 = (uiNext_2-1)%SNOWFLAKE_ARM_NUMBER; } if (uiLoop_3 >= SNOWFLAKE_ARM_NUMBER-1) { printf("Twin snowflakes found./n"); return TWIN_SNOWFLAKES_FOUND; } } } } /*next*/ uiNextNodeIndex = gastSnowflake.astSnowFlake[uiNextNodeIndex].uiNextNodeIndex; if (0 == uiNextNodeIndex) { gastSnowflake.astSnowFlake[uiNextNodeIndex].uiNextNodeIndex = uiLoopSnowflake; break; } } } else { gauiSnowflakeHash[uiHashIndex] = uiLoopSnowflake; } } return NO_TWIN_SNOWFLAKES_FOUND; } int SearchTwinSnowflakeMain() { SnowflakeInput(); if(NO_TWIN_SNOWFLAKES_FOUND == InitSnowflakeHashAndSearch()) { printf("No two snowflakes are alike./n"); } return 0; }