标签:des style io color ar os sp for strong
1 2 1 2 3 4 5 6 4 3 2 1 6 5
Twin snowflakes found.
题意:雪花有六个角,分别赋给他们长度,依照顺时针输入,问你在输入的雪花中有没有全然一样的.
分析:依照传统的做法时间是O(n^2),由于数据非常大所以说会超时,要换一种方法,要用到散列表(大神们讲的非常具体,我就现丑了)。
这道题的比較也蛮奇特的。
代码1(链表形式):
#include <cstdio> #include <cstring> #define M 20005 using namespace std; struct node { int a[6]; struct node *next; /* data */ }; node *s[M]; int match(int *temp, int sum){ int i, j; node *p; p = s[sum]->next; while(p){ for(i = 0; i < 6; ++ i){ for(j = 0; j < 6; ++ j){ if(temp[j] != p->a[(i+j)%6]) break; } if(j == 6) return true; for(j = 0; j < 6; ++ j){ if(temp[j] != p->a[(i+6-j)%6]) break; } if(j == 6) return true; } p = p->next; } p = new node; for(i = 0; i < 6; ++ i) p->a[i] = temp[i]; p->next = s[sum]->next; s[sum]->next = p; return false; } int main(){ int t, n, i, j, temp[6]; scanf("%d", &t); while(t --){ int sum,flag = 0; scanf("%d", &n); for(i = 0; i < M; ++ i){ s[i] = new node; s[i]->next = NULL; } while(n --){ sum = 0; for(i = 0; i < 6; ++i){ scanf("%d", &temp[i]); sum += temp[i]; } sum %= M; if(!flag){ if(match(temp, sum)) flag = 1; } } if(flag) puts("Twin snowflakes found."); else puts("No two snowflakes are alike."); } return 0; }代码2(三维数组):
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define M 20000 int s[M][100][6]; int len[M]; int match(int *a, int *b){ int i, j, k; for(i = 0; i < 6; i ++){ for(j = 0; j < 6; j ++){ if(a[j] != b[(i+j)%6]) break; } if(j == 6) return true; for(j = 0; j < 6; j ++){ if(a[j] != b[(i+6-j)%6]) break; } if(j == 6) return true; } return false; } int main(){ int t, n, sum, temp[6]; scanf("%d", &t); while(t --){ int i, j, k, flag = 0; scanf("%d", &n); memset(len, 0, sizeof(int)*(M+1)); while(n --){ sum = 0; for(i = 0; i < 6; i ++){ scanf("%d",&temp[i]); sum += temp[i]; } sum %= M; for (i = 0; i < 6; ++i){ s[sum][len[sum]][i] = temp[i]; } ++len[sum]; } for(i = 0; i < M; i ++){ if(len[i] >1) for(j = 0; j < len[i]-1; j ++){ for(k = j+1; k < len[i]; k ++){ if(match(s[i][j], s[i][k])){ flag = 1; break; } } if(flag) break; } if(flag) break; } if(flag) puts("Twin snowflakes found."); else puts("No two snowflakes are alike."); } return 0; }
标签:des style io color ar os sp for strong
原文地址:http://www.cnblogs.com/yxwkf/p/4083979.html