745. Prefix and Suffix Search
来源:互联网 发布:新材料在线软件 编辑:程序博客网 时间:2024/06/06 08:29
原网址为 https://leetcode.com/problems/prefix-and-suffix-search/description/
Given many words
, words[i]
has weight i
.
Design a class WordFilter
that supports one function, WordFilter.f(String prefix, String suffix)
. It will return the word with given prefix
and suffix
with maximum weight. If no word exists, return -1.
Examples:
Input:WordFilter(["apple"])WordFilter.f("a", "e") // returns 0WordFilter.f("b", "") // returns -1
Note:
words
has length in range[1, 15000]
.- For each test case, up to
words.length
queriesWordFilter.f
may be made. words[i]
has length in range[1, 10]
.prefix, suffix
have lengths in range[0, 10]
.words[i]
andprefix, suffix
queries consist of lowercase letters only.
代码:
class TrieNode {public: TrieNode() { children.resize(27, NULL); weight = 0; }public: vector<TrieNode*> children; int weight;};class WordFilter {public: WordFilter(vector<string> words) { root = new TrieNode(); for (int weight = 0; weight < words.size(); weight++) { string word = words[weight] + "{"; for (int i = 0; i < word.size(); i++) { TrieNode *cur = root; cur->weight = weight; for (int j = i; j < 2 * word.size() - 1; j++) { int k = word[j % word.size()] - 'a'; if (cur->children[k] == NULL) { cur->children[k] = new TrieNode(); } cur = cur->children[k]; cur->weight = weight; } } } } int f(string prefix, string suffix) { TrieNode *cur = root; string q = suffix + "{" + prefix; for (int i = 0; i < q.size(); i++) { if (cur->children[q[i] - 'a'] == NULL) { return -1; } cur = cur->children[q[i] - 'a']; } return cur->weight; }public: TrieNode *root;};
阅读全文
0 0
- 745. Prefix and Suffix Search
- 745. Prefix and Suffix Search
- 745. Prefix and Suffix Search
- LWC 62:745. Prefix and Suffix Search
- leetCode 745. Prefix and Suffix Search的思路
- Table 9-1 -- Bit strings with "prefix" and "suffix" bits and assigment to codeNum rangs
- <trim>: prefix+prefixOverrides+suffix+suffixOverrides
- mybatis prefix,prefixOverrides,suffix,suffixOverrides
- <trim>: prefix+prefixOverrides+suffix+suffixOverrides
- Exam 2 Maximum Xor with Prefix and Suffix - Works Application 16
- java web配置prefix suffix的是什么意思
- Difference between "Primary DNS Suffix" and "Connection specific DNS Suffix"?
- SpringMVC - 视图解析器的 prefix和suffix
- [2_3_prefix] Improvement: Space: only store suffix/prefix; Time: utilize Trie structure
- poj 2752 KMP的next数组应用(prefix-suffix string)
- mybatis之<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
- mybatis中的<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>说明
- mybatis之<trim prefix="" suffix="" suffixOverrides="" prefixOverrides=""></trim>
- 提高SQL查询性能的7个法宝
- 55-Jump Game
- Python3.6使用tesseract-ocr的正确姿势
- spark运行架构 storm 流数据处理
- JSP页面如何导入优酷视频
- 745. Prefix and Suffix Search
- 关于extern以及源文件和头文件的应用
- 以太币挖矿教程
- zabbix 安装
- 经典算法之暴力匹配算法(字符串)
- leetcode 535. Encode and Decode TinyURL 编码和解码精简URL地址
- JNI基础知识
- shiro权限定义的三种方法
- java基础之单例模式