-
Notifications
You must be signed in to change notification settings - Fork 0
/
findNumOfValidWords.cpp
90 lines (90 loc) · 2.03 KB
/
findNumOfValidWords.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
vector<int> ret;
/*void fun(vector<int>& wordMask,vector<int>& puzzleMask,vector<string>& puzzles)
{
for(int word:wordMask)
for(int i=0;i<puzzleMask.size();++i)
{
int puzzle=puzzleMask[i];
int firstChar=1<<(puzzles[i][0]-'a');
if(((puzzle&word)==word)&&((word&firstChar)!=0))
ret[i]++;
}
}*/
void fun(map<int,int>& wordMask,vector<int>& puzzleMask,vector<string>& puzzles)
{
for(int i=0;i<puzzleMask.size();++i)
{
int puzzle=puzzleMask[i];
int firstChar=1<<(puzzles[i][0]-'a');
for(auto itr=wordMask.begin();itr!=wordMask.end();++itr)
{
int word=itr->first;
if(((puzzle&word)==word)&&((word&firstChar)!=0))
ret[i]+=itr->second;
}
}
}
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles)
{
vector<int> puzzleMask;
for(string s:puzzles)
{
int mask=0;
for(char x:s)
mask=mask|1<<(x-'a');
puzzleMask.push_back(mask);
}
ret.resize(puzzles.size(),0);
/*vector<int> wordMask;
for(string s: words)
{
int mask=0;
for(char x:s)
mask=mask|1<<(x-'a');
wordMask.push_back(mask);
}*/
map<int,int> wordMask;
for(string s:words)
{
int mask=0;
for(char x:s)
mask=mask|1<<(x-'a');
wordMask[mask]++;
}
fun(wordMask,puzzleMask,puzzles);
return ret;
}
//
vector<int> ret;
int dp(int mask,string str,int index,map<int,int>& s)
{
if(index>=str.length())
return 0;
int q=dp(mask,str,index+1,s);
mask=mask|1<<(str[index]-'a');
q+=dp(mask,str,index+1,s);
if(s.find(mask)!=s.end())
return q+s[mask];
else return q;
}
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles)
{
ret.resize(puzzles.size(),0);
map<int,int> s;
for(string str:words)
{
int mask=0;
for(char x:str)
mask=mask|1<<(x-'a');
s[mask]++;
}
for(int i=0;i<puzzles.size();++i)
{
string str=puzzles[i];
int mask=0;mask|=1<<(str[0]-'a');
if(s.find(mask)!=s.end())
ret[i]=s[mask];
ret[i]+=dp(mask,str,1,s);
}
return ret;
}