poj3349(hash or violence)
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 38600 | Accepted: 10120 |
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<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<ctime> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<stack> #include<queue> using namespace std; #define inf 2000000000 #define linf 99999999999999 #define ll long long #define N 1000010 #define mod 999983 #define md 10003 #define mx 100037 #define debug(x) cout<<"debug: "<<x<<endl inline const int read(){ register int x=0,f=1; register char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline const char in(){ for(register char ch=getchar();;ch=getchar()) if(ch>='A'&&ch<='Z') return ch; } int n,a[7]; ll w[N]; void deal(int k){ ll res=0,ans=1; //for(int i=6;i;i--) res=res+a[i]%mod; //res=res%mod+1; for(int i=6;i;i--) res=res+a[i]%mx; for(int i=6;i;i--) ans=ans*a[i]%mod; w[k]=(res*md%mod+1)*(ans*md%mod+1); } int main(){ //freopen("sh.txt","r",stdin); n=read(); for(int i=1;i<=n;i++){ //memset(a,-1,sizeof a); for(int j=1;j<=6;j++) a[j]=read(); deal(i); } stable_sort(w+1,w+n+1); int t=unique(w+1,w+n+1)-(w+1); //for(int i=t;i<=n;i++) printf("%d %d\n",i,w[i]); if(t<n) puts("Twin snowflakes found."); else puts("No two snowflakes are alike."); return 0; }
上述代码,hash离散化程度太高,导致数据较大时,基本上匹配不上。
所以干脆不用hash,裸暴力就可以了。
思路清晰,看代码就好了。
AC代码1:
//请用c++提交 #include<cstdio> #include<cmath> #include<cstring> #include<iostream> #include<algorithm> using namespace std; #define N 100010 struct node{ int e[6]; void is(){ sort(e,e+6); } void get(){ scanf("%d%d%d%d%d%d",&e[0],&e[1],&e[2],&e[3],&e[4],&e[5]); } }snow[N]; int n; bool flag; bool cmp(const node &a,const node &b){ for(int i=0;i<6;i++){ if(a.e[i]==b.e[i]) continue; return a.e[i]<b.e[i]; } return flag=1; } int main(){ scanf("%d",&n); for(int i=0;i<n;i++){ snow[i].get(); snow[i].is(); } flag=0; sort(snow,snow+n,cmp); if(flag) printf("Twin snowflakes found.\n"); else printf("No two snowflakes are alike.\n"); return 0; }
当然你非要hash的代码,shenben也奉上,但不做解释。
AC代码2:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=1200010; const int H=1200007; struct Node{ int num[6]; int next; }node[N]; int cur; int hashTable[H]; unsigned int getHash(int* num){ unsigned int hash=0; for(int i=0;i<6;++i) hash+=num[i]; return hash%H; } bool cmp(int* num1,int* num2){ for(int i=0;i<6;++i) if(num1[i]!=num2[i]) return false; return true; } void insertHash(int* num,unsigned int h){ for(int i=0;i<6;++i) node[cur].num[i]=num[i]; node[cur].next=hashTable[h]; hashTable[h]=cur; ++cur; } bool searchHash(int* num){ unsigned h=getHash(num); int next=hashTable[h]; while(next!=-1){ if(cmp(num,node[next].num)) return true; next=node[next].next; } insertHash(num,h); return false; } int main(){ int num[2][12]; int n; bool twin=false; memset(hashTable,-1,sizeof hashTable); scanf("%d",&n); while(n--){ for(int i=0;i<6;i++) scanf("%d",&num[0][i]),num[0][i+6]=num[0][i]; if(twin) continue; for(int i=0;i<6;i++) num[1][i+6]=num[1][i]=num[0][5-i]; for(int i=0;i<6;i++){ if(searchHash(num[0]+i)||searchHash(num[1]+i)){ twin=true; break; } } } if(twin) printf("Twin snowflakes found.\n"); else printf("No two snowflakes are alike.\n"); return 0; }