hdu-1800(字串hash)
阿新 • • 發佈:2019-01-02
題目連結:傳送門
思路:
就是找最多多少個掃帚,每個掃帚上有連續遞增的序列,就是找一個最多重複數字的重複次數。
由於是30位,每次用char*型別,然後用hash對映一下,排序找最多就行了。
注意:
(1)num最小也是1。
(2)注意前導零。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 30030; const int INF = 0x7ffffffView Code; typedef unsigned long long ULL; ULL base=133,a[maxn]; char str[maxn]; ULL Hash(char *s) { ULL ans=0,H=0; while(*s=='0') s++; while(*s) { H=H*base+(*s++); } return H&INF; } int main(void) { int n,i; while(~scanf("%d",&n)) { for(i=0;i<n;i++) { scanf("%s",&str); a[i]=Hash(str); } sort(a,a+n); int num=1,tp=1; for(i=1;i<n;i++) { if(a[i]==a[i-1]) { tp++; if(num<tp) num=tp; } else tp=1; } printf("%d\n",num); } return 0; }