class Solution {
public:
vector<vector<string>> res;
vector<string> path;
vector<vector<string>>partition(string s){dfs("",0, s);return res;}
bool check(string & now){if(now.empty())return false;for(int i =0, j = now.size()-1; i < j; i ++, j --)if(now[i]!= now[j])return false;return true;}voiddfs(string now,int u, string &s){if(u == s.size()){if(check(now)){
path.push_back(now);
res.push_back(path);
path.pop_back();}return;}if(check(now)){
path.push_back(now);dfs("", u, s);
path.pop_back();}dfs(now + s[u], u +1, s);}};
class Solution {
public:intminCut(string s){int n = s.size();
vector<int>f(n +1);
vector<vector<bool>>st(n, vector<bool>(n, false));for(int i =0; i < n; i ++)for(int j = i;~j; j --)if(i - j <=1) st[j][i]= s[j]== s[i];else st[j][i]= s[j]== s[i]&& st[j +1][i -1];
f[0]=0;for(int i =1; i <= n; i ++){
f[i]= INT_MAX;for(int j =0; j < i; j ++)if(st[j][i -1])
f[i]=min(f[i], f[j]+1);}returnmax(0, f[n]-1);}};
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/
class Solution {
public:
unordered_map<Node*, Node*> hash;
Node*cloneGraph(Node* node){if(!node)return node;//创建新点auto p = new Node(node->val);
hash[node]= p;dfs(node);return p;}voiddfs(Node* node)//遍历整个图{for(auto ver : node->neighbors)//遍历邻接节点{if(!hash.count(ver))//没有节点 先创建{
hash[ver]= new Node(ver->val);dfs(ver);}
hash[node]->neighbors.push_back(hash[ver]);}}};
class Solution {
public:intcanCompleteCircuit(vector<int>& gas, vector<int>& cost){int n = gas.size();for(int i =0, j; i < n; i += j +1)//i += j + 1{int gas_left =0;for(j =0; j < n; j ++){int k =(i + j)% n;
gas_left += gas[k]- cost[k];if(gas_left <0)break;}if(j >= n)return i;}return-1;}};
class Solution {
public:intcandy(vector<int>& ratings){int n = ratings.size(), res =0;if(n <2)return n;
vector<int>f(n,1),g(n,1);for(int i =1; i < n; i ++)if(ratings[i -1]< ratings[i])
f[i]= f[i -1]+1;for(int i = n -2;~i; i --)if(ratings[i]> ratings[i +1])
g[i]= g[i +1]+1;for(int i =0; i < n; i ++)
res +=max(f[i], g[i]);return res;}};
class Solution {
public:intsingleNumber(vector<int>& nums){int res =0;for(auto x : nums) res ^= x;return res;}};
class Solution {
public:intsingleNumber(vector<int>& nums){int res =0;for(int bit =0; bit <32; bit ++){int count =0;for(int i =0; i < nums.size(); i ++)
count +=(nums[i]>> bit)&1;//统计第bit位 1 的个数
res +=(count %3)<< bit;}return res;}};
/*
// Definition for a Node.
class Node {
public:
int val;
Node* next;
Node* random;
Node(int _val) {
val = _val;
next = NULL;
random = NULL;
}
};
*/
class Solution {
public:
Node*copyRandomList(Node* head){if(!head)return head;
Node *p = head,*tempP;while(p){
tempP = new Node(p->val, p->next, p->random);
tempP->next = p->next;
p->next = tempP;
p = p->next->next;}//复制random
p = head;while(p){if(p->random){
p->next->random = p->random->next;}else
p->next->random =NULL;
p = p->next->next;}
p = head;
Node* copyHead = p->next;while(p){
tempP = p->next;
p->next = p->next->next;if(tempP->next)
tempP->next = tempP->next->next;
p = p->next;}return copyHead;}};
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict){
unordered_set<string>S;for(auto word : wordDict) S.insert(word);int n = s.size();
vector<bool>f(n +1, false);
f[0]= true;for(int i =1; i <= n; i ++)for(int j =0; j < i; j ++)if(S.count(s.substr(j, i - j))&& f[j]){
f[i]= true;break;}return f[n];}};
class Solution {
public:
vector<string>res;
unordered_map<string,int>dict;
vector<string>wordBreak(string s, vector<string>& wordDict){for(auto&word : wordDict) dict[word]=1;int n = s.size();
vector<bool>f(n +1, true);for(int i =1; i <= n; i ++){
f[i]= false;for(int j =0; j < i; j ++)if(dict[s.substr(j, i - j)]&& f[j]){
f[i]= true;break;}}dfs(f, s,"", n);return res;}voiddfs(vector<bool>&f, string &s, string path,int u){if(!u){
res.push_back(path.substr(0, path.size()-1));//去除path最后面空格return;}for(int i =0; i < u; i ++)if(dict[s.substr(i, u - i)]&& f[i])dfs(f, s, s.substr(i, u - i)+" "+ path, i);}};