Leetcode-5176 Number of Valid Words for Each Puzzle(猜字谜)

 1 #define _for(i,a,b) for(int i = (a);i < b;i ++)
 2 #define _rep(i,a,b) for(int i = (a);i > b;i --)
 3 class Solution
 4 {
 5     public:
 6         vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles)
 7         {
 8             map<int,int> m;
 9             _for(i,0,words.size())
10             {
11                 int st = 0;
12                 int cnt = 0;
13                 _for(j,0,words[i].size())
14                     st |= 1<<words[i][j]-'a';
15                 m[st] ++;
16             }
17             vector<int> vV;
18 
19             _for(i,0,puzzles.size())
20             {
21                 int sup = 0;
22                 int rnt = 0;
23                 _for(j,0,puzzles[i].size())
24                     sup |= 1<<puzzles[i][j]-'a';
25                 int sub = sup;
26                 do
27                 {
28                     if((sub & (1<<puzzles[i][0]-'a')))
29                         rnt += m[sub];
30                     sub = (sub-1) & sup;
31                 }
32                 while(sub != sup);   // 处理完之后 会有 -1&sup = sup
33                 vV.push_back(rnt);
34             }
35             return vV;
36         }
37 };

 

posted @ 2019-09-01 13:08  Asurudo  阅读(241)  评论(0编辑  收藏  举报