码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-09-01 14:53:01      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:turn   ring   rds   +=   int   bsp   string   size   push   

 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 };

 

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

标签:turn   ring   rds   +=   int   bsp   string   size   push   

原文地址:https://www.cnblogs.com/Asurudo/p/11442047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!