Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- vector<string> words, puzzles;
- vector<int> findNumOfValidWords(vector<string> &words, vector<string> &puzzles){
- map<int, int> mp;
- for(string &str : words){
- int btw = 0;
- for(int i = 0; i < str.size(); ++i){
- btw |= (1 << (str[i] - 'a'));
- }
- ++mp[btw];
- }
- vector<int> ans;
- for(string &str : puzzles){
- int sum = 0;
- int btw = 0;
- int req = 1 << (str[0] - 'a');
- for(int i = 1; i < str.size(); ++i){
- btw |= (1 << (str[i] - 'a'));
- }
- for(int b = btw; b != 0; b = (b - 1) & btw){
- if(mp.find(b | req) != mp.end()){
- sum += mp[b | req];
- }
- }
- if(mp.find(req) != mp.end()){
- sum += mp[req];
- }
- ans.push_back(sum);
- }
- return ans;
- }
- int main(){
- int nWord, nPuzzle;
- scanf("%d%d", &nWord, &nPuzzle);
- for(int i = 1; i <= nWord; ++i){
- string str;
- cin >> str;
- words.push_back(str);
- }
- for(int i = 1; i <= nPuzzle; ++i){
- string str;
- cin >> str;
- puzzles.push_back(str);
- }
- for(int x : findNumOfValidWords(words, puzzles)){
- cout << x << ' ';
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement