力扣1178. 猜字谜-C语言实现-困难题

179 阅读1分钟

题目

传送门

文本

外国友人仿照中国字谜设计了一个英文版猜字谜小游戏,请你来猜猜看吧。 字谜的迷面 puzzle 按字符串形式给出,如果一个单词 word 符合下面两个条件,那么它就可以算作谜底:

单词 word 中包含谜面 puzzle 的第一个字母。
单词 word 中的每一个字母都可以在谜面 puzzle 中找到。
例如,如果字谜的谜面是 "abcdefg",那么可以作为谜底的单词有 "faced", "cabbage", 和 "baggage";而 "beefed"(不含字母 "a")以及 "based"(其中的 "s" 没有出现在谜面中)。

返回一个答案数组 answer,数组中的每个元素 answer[i] 是在给出的单词列表 words 中可以作为字谜迷面 puzzles[i] 所对应的谜底的单词数目。

示例:

输入: words = ["aaaa","asas","able","ability","actt","actor","access"], puzzles = ["aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"] 输出:[1,1,3,2,4,0] 解释: 1 个单词可以作为 "aboveyz" 的谜底 : "aaaa" 1 个单词可以作为 "abrodyz" 的谜底 : "aaaa" 3 个单词可以作为 "abslute" 的谜底 : "aaaa", "asas", "able" 2 个单词可以作为 "absoryz" 的谜底 : "aaaa", "asas" 4 个单词可以作为 "actresz" 的谜底 : "aaaa", "asas", "actt", "access" 没有单词可以作为 "gaswxyz" 的谜底,因为列表中的单词都不含字母 'g'。

提示:

1 <= words.length <= 10^5
4 <= words[i].length <= 50
1 <= puzzles.length <= 10^4
puzzles[i].length == 7
words[i][j], puzzles[i][j] 都是小写英文字母。
每个 puzzles[i] 所包含的字符都不重复。

来源:力扣(LeetCode)

模板

 */
int* findNumOfValidWords(char ** words, int wordsSize, char ** puzzles, int puzzlesSize, int* returnSize){

}

解题

分析

本题所需要的是对于元素的核查,不考虑遂于元素的个数核查,只考虑对于元素是否存在的核查即可。谜面与谜底之间的联系是:谜底的组成字母一定在谜面内存在。如果存在对于元素的核查里就可以加1处理来统计对应的谜面下能推得的谜底的个数。

完整源码

题解传送门

struct unordered_map {
    int key, val;
    UT_hash_handle hh;
};

int* findNumOfValidWords(char** words, int wordsSize, char** puzzles, int puzzlesSize, int* returnSize) {
    struct unordered_map* frequency = NULL;

    for (int i = 0; i < wordsSize; i++) {
        int n = strlen(words[i]);
        int mask = 0;
        for (int j = 0; j < n; j++) {
            mask |= (1 << (words[i][j] - 'a'));
        }
        if (__builtin_popcount(mask) <= 7) {
            struct unordered_map* tmp;
            HASH_FIND_INT(frequency, &mask, tmp);
            if (tmp == NULL) {
                tmp = malloc(sizeof(struct unordered_map));
                tmp->key = mask;
                tmp->val = 1;
                HASH_ADD_INT(frequency, key, tmp);
            } else {
                tmp->val++;
            }
        }
    }

    int* ans = malloc(sizeof(int) * puzzlesSize);
    *returnSize = 0;

    for (int i = 0; i < puzzlesSize; i++) {
        int total = 0;

        // 枚举子集方法一
        // for (int choose = 0; choose < (1 << 6); ++choose) {
        //     int mask = 0;
        //     for (int j = 0; j < 6; ++j) {
        //         if (choose & (1 << j)) {
        //             mask |= (1 << (puzzles[i][j + 1] - 'a'));
        //         }
        //     }
        //     mask |= (1 << (puzzles[i][0] - 'a'));
        //     struct unordered_map* tmp;
        //     HASH_FIND_INT(frequency, &mask, tmp);
        //     if (tmp != NULL) {
        //         total += tmp->val;
        //     }
        // }

        // 枚举子集方法二
        int mask = 0;
        for (int j = 1; j < 7; ++j) {
            mask |= (1 << (puzzles[i][j] - 'a'));
        }
        int subset = mask;
        do {
            int s = subset | (1 << (puzzles[i][0] - 'a'));
            struct unordered_map* tmp;
            HASH_FIND_INT(frequency, &s, tmp);
            if (tmp != NULL) {
                total += tmp->val;
            }
            subset = (subset - 1) & mask;
        } while (subset != mask);
        ans[(*returnSize)++] = total;
    }
    return ans;
}

作者:LeetCode-Solution

运行结果

在这里插入图片描述