class WordDictionary {
public:struct Node
{
bool is_end;
Node *next[26];Node(){
is_end = false;for(int i =0; i <26; i ++)
next[i]=0;}};
Node *root;/** Initialize your data structure here. */WordDictionary(){
root = new Node();}/** Adds a word into the data structure. */voidaddWord(string word){
Node *p = root;for(char c : word){int son = c -'a';if(!p->next[son]) p->next[son]= new Node();
p = p->next[son];}
p->is_end = true;}/** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
bool search(string word){returndfs(word,0, root);}
bool dfs(string &word,int k, Node *u){if(k == word.size())return u->is_end;if(word[k]!='.'){if(u->next[word[k]-'a'])returndfs(word, k +1, u->next[word[k]-'a']);}else{for(int i =0; i <26; i ++)if(u->next[i]){if(dfs(word, k +1, u->next[i]))return true;}}return false;}};/**
* Your WordDictionary object will be instantiated and called as such:
* WordDictionary* obj = new WordDictionary();
* obj->addWord(word);
* bool param_2 = obj->search(word);
*/
class Solution {
public:struct Node
{int id;
Node *next[26];Node(){
id =-1;for(int i =0; i <26; i ++)
next[i]=0;}};
Node *root;voidinsert(string word,int id){
Node *p = root;for(char c : word){int son = c -'a';if(!p->next[son]) p->next[son]= new Node();
p = p->next[son];}
p->id = id;}
unordered_set<string>hash;
vector<string>ans;
vector<vector<bool>>st;
vector<string>_words;int n, m;
vector<string>findWords(vector<vector<char>>& board, vector<string>& words){if(board.empty())return ans;
_words = words;
root = new Node();for(int i =0; i < words.size(); i ++)insert(words[i], i);
n = board.size(), m = board[0].size();
st = vector<vector<bool>>(n, vector<bool>(m, false));for(int i =0; i < n; i++)for(int j =0; j < m; j ++)dfs(board, i, j, root->next[board[i][j]-'a']);return ans;}voiddfs(vector<vector<char>>&board,int x,int y, Node *u){if(!u)return;
st[x][y]= true;if(u->id !=-1){
string match = _words[u->id];if(!hash.count(match)){
hash.insert(match);
ans.push_back(match);}}int dx[4]={-1,0,1,0}, dy[4]={0,1,0,-1};for(int i =0; i <4; i ++){int a = x + dx[i], b = y + dy[i];if(a >=0&& a < n && b >=0&& b < m &&!st[a][b]){char c = board[a][b];dfs(board, a, b, u->next[c -'a']);}}
st[x][y]= false;}};
class Solution {
public:introb(vector<int>& nums){int n = nums.size();if(n ==1)return nums[0];
vector<int>f(n +1,0),g(n +1,0);for(int i =2; i <= n; i ++)//第一个没取{
f[i]= g[i -1]+ nums[i -1];
g[i]=max(f[i -1], g[i -1]);}int res =max(f[n], g[n]);for(int i =1; i <= n; i ++)//第一个取了{
f[i]= g[i -1]+ nums[i -1];
g[i]=max(f[i -1], g[i -1]);}returnmax(res, g[n]);}};
class Solution {
public:
string shortestPalindrome(string s){
string ls = s;reverse(s.begin(), s.end());
s = ls +'#'+ s;int n = s.size();
vector<int>next(n +1,0);//next 数组 求前面和后面相同的最长长度for(int i =2, j =0; i <= n; i ++)//kmp{while(j && s[j]!= s[i -1]) j = next[j];if(s[j]== s[i -1]) j ++;
next[i]= j;}int res = next[n];
string sup = ls.substr(res);reverse(sup.begin(), sup.end());return sup + ls;}};
class Solution {
public:intfindKthLargest(vector<int>& nums,int k){
priority_queue<int, vector<int>, greater<int>> Q;for(int i =0; i < nums.size(); i ++){if(Q.size()< k)
Q.push(nums[i]);elseif(Q.top()< nums[i]){
Q.pop();
Q.push(nums[i]);}}return Q.top();}};
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
vector<vector<int>>combinationSum3(int k,int n){dfs(k, n,1);return res;}voiddfs(int k,int n,int start){if(!k){if(!n) res.push_back(path);return;}for(int i = start; i <=10- k; i ++){if(n >= i){
path.push_back(i);dfs(k -1, n - i, i +1);
path.pop_back();}}}};
class Solution {
public:
bool containsDuplicate(vector<int>& nums){
unordered_set<int>hash;for(int i =0; i < nums.size(); i ++){if(hash.find(nums[i])!= hash.end())return true;
hash.insert(nums[i]);}return false;}};
class Solution {
public:
bool containsDuplicate(vector<int>& nums){if(nums.size()<1)return false;sort(nums.begin(), nums.end());for(int i =0; i < nums.size()-1; i ++){if(nums[i]== nums[i +1])return true;}return false;}};
class Solution {
public:
vector<vector<int>>getSkyline(vector<vector<int>>& buildings){
vector<vector<int>> res;
multiset<pair<int,int>> events;
multiset<int> height;
height.insert(0);int n = buildings.size();//使用扫描线,从左至右扫过。如果遇到左端点,将高度入堆,如果遇到右端点,则将高度从堆中删除。for(int i =0; i < n; i ++){
events.insert(make_pair(buildings[i][0],-buildings[i][2]));
events.insert(make_pair(buildings[i][1], buildings[i][2]));}for(auto p : events){if(p.second <0)//左端点,高度大于当前位置最大高度加入答案,同时加入height{if(-p.second >*height.rbegin())
res.push_back({p.first,-p.second});
height.insert(-p.second);}else//右端点,先移除height,如果高度高于新的height的最大值,是关键点{
height.erase(height.find(p.second));if(p.second >*height.rbegin())
res.push_back({p.first,*height.rbegin()});}}return res;}};
class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums,int k){
unordered_map<int,int>hash;for(int i =0; i < nums.size(); i ++){if(hash.count(nums[i])&& i - hash[nums[i]]<= k)return true;
hash[nums[i]]= i;}return false;}};
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums,int k,int t){
set<longlong> Set;for(int i =0; i < nums.size(); i ++){auto s = Set.lower_bound(nums[i]-(longlong)t);//找出>=nums[i] - t 的最小元素返回其下标if(s != Set.end()&&*s <=(longlong)nums[i]+(longlong)t)return true;
Set.insert(nums[i]);//如果树的大小超过了k, 则移除最早加入树的那个数if(Set.size()> k)
Set.erase(nums[i - k]);}return false;}};