Hash表
Hash最好用map+unsigned long long 写,不用取模,自动溢出
#define ULL unsigned long long
map<ULL,int> Map;
while(pd(ch=getchar())){
if(ch>='A'&&ch<='Z') ch=ch+'a'-'A';
sum=sum*31+ch;
}
Map[sum]++;
for(map<ULL,int>::iterator i=Map.begin();i!=Map.end();i++)
int cur=i->second;
代码
#include<bits/stdc++.h>
#define ULL unsigned long long
using namespace std;
map<ULL,int> Map;
bool pd(char ch){
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) return true;
return false;
}
bool read(){
char ch; ULL sum=0;
while(!pd(ch=getchar())) if(ch==EOF) return 0;
if(ch>='A'&&ch<='Z') ch=ch+'a'-'A';
sum=sum*31+ch;
while(pd(ch=getchar())){
if(ch>='A'&&ch<='Z') ch=ch+'a'-'A';
sum=sum*31+ch;
}
Map[sum]++;
return 1;
}
int main()
{
int n,ans=0,cnt=0;
while(read()) cnt++;
for(map<ULL,int>::iterator i=Map.begin();i!=Map.end();i++){
int cur=i->second;
ans+=cur*cur*cur*cur;
}
cout<<ans<<" "<<cnt;
return 0;
}