短文评估【安徽省选2003】- hash / trie
题目分析
其实是hash/trie裸题,讲一下hash的做法:如果其小写状态是第一次出现则加入集合,同时将小写状态加入小写单词的hash表,最后查时查出出现次数即可。
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<set>
using namespace std;
const int L = 40050;
typedef unsigned long long ull;
const ull Mod = 23333, H = 31;
ull oriHash, lowHash;
char s;
typedef long long ll;
ll q, p;
typedef pair<ull, ll> P;
vector<P> lowhash[Mod + 5];
vector<ull> S;
inline void low_insert(ull x){
int key = x % Mod;
for(int e = 0; e < lowhash[key].size(); e++)
if(lowhash[key][e].first == x){
lowhash[key][e].second++;
return;
}
lowhash[key].push_back(P(x, 1));
}
inline ll low_query(ull x){
int key = x % Mod;
for(int e = 0; e < lowhash[key].size(); e++)
if(lowhash[key][e].first == x)
return lowhash[key][e].second;
return 0;
}
inline void wr(ll x){
if(x < 0) putchar('-'), x = -x;
if(x > 9) wr(x / 10);
putchar(x % 10 + '0');
}
int main(){
while(scanf("%c", &s) != EOF){
if('a' <= s&& s <= 'z'){
lowHash = lowHash * H + (s - 'a' + 1);
continue;
}
else if('A' <= s && s <= 'Z'){
lowHash = lowHash * H + (s - 'A' + 1);
continue;
}
// cout<<tmp<<" "<<q<<endl;
if(lowHash){
q++;
if(!low_query(lowHash)) S.push_back(lowHash);
low_insert(lowHash);
lowHash = 0;
}
}
if(lowHash){
q++;
if(!low_query(lowHash)) S.push_back(lowHash);
lowHash = 0;
}
for(int i = 0; i < S.size(); i++){
ull x = S[i];
ll tt = low_query(x);
// cout<<tt<<endl;
p += pow(tt, 4);
}
wr(p), putchar(' '), wr(q);
return 0;
}