-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringCount.cpp
56 lines (52 loc) · 943 Bytes
/
StringCount.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
const int N=666013,x=13;
vector <pair <string,int>> h[N];
int hash_key(string s)
{
int l=s.size(),key=0;
for(int i=0;i<l;i++)
key=(key*x+s[i])%N;
return key;
}
void add(string s)
{
int key=hash_key(s);
for(auto &x:h[key])
{
if(x.first==s)
{
x.second++;
return;
}
}
h[key].push_back(make_pair(s,1));
return;
}
int counts(string s)
{
int key=hash_key(s);
for(auto x:h[key])
if(x.first==s)
return x.second;
return 0;
}
int main()
{
int n;
string s,t;
cout << "Enter no ";
cin>>n;
cout<<"Enter Strings :"<< endl;
for(int i=0;i<n;i++)
{
cin>>s;
add(s);
}
cout<<"Enter target :"<<endl;
cin>>t;
cout<<counts(t);
return 0;
}