标签:push containe 切分 ble 苹果 搜索 css ref ini
这个没什么好说的
class Solution { public: vector<string> buildArray(vector<int>& target, int n) { vector<string> ans; int j=1; for(int i=0;i<target.size();i++){ while(j<=n&&j!=target[i]){ ans.push_back("Push"); ans.push_back("Pop"); j++; } if(j>n)break; ans.push_back("Push");j++; } return ans; } };
用的相当暴力的方法,dp保留了一下元素i到j异或的值,1800ms+
class Solution { public: int countTriplets(vector<int>& arr) { vector<vector<int>> dp(arr.size(),vector<int>(arr.size(),0)); int ans=0; for(int i=0;i<arr.size();i++)dp[i][i]=arr[i]; for(int i=0;i<arr.size()-1;i++){ for(int j=i+1;j<arr.size();j++){ for(int k=j;k<arr.size();k++){ int a,b; if(dp[i][j-1]!=0)a=dp[i][j-1]; else{ int tmp=0; for(int t=i;t<=j-1;t++){ tmp^=arr[t]; } dp[i][j-1]=tmp; a=tmp; } if(dp[j][k]!=0)b=dp[j][k]; else{ int tmp=0; for(int t=j;t<=k;t++){ tmp^=arr[t]; } dp[j][k]=tmp; b=tmp; } if(a==b)++ans; } } } return ans; } };
DFS,把所有到达苹果的路径上的点的数量加起来*2
class Solution { public: void dfs(int root,vector<bool>& res, unordered_map<int,vector<int>>& mp, vector<bool>& hasApple,vector<bool>& vis,vector<int> tmp){ if(hasApple[root]){ for(int x:tmp)res[x]=true; } for(int v:mp[root]){ if(!vis[v]){ vis[v]=1; tmp.push_back(v); dfs(v,res,mp,hasApple,vis,tmp); tmp.pop_back(); vis[v]=0; } } } int minTime(int n, vector<vector<int>>& edges, vector<bool>& hasApple) { unordered_map<int,vector<int>> mp; for(int i=0;i<edges.size();i++){ mp[edges[i][0]].push_back(edges[i][1]); } vector<bool> res(n,false); vector<bool> vis(n,false); dfs(0,res,mp,hasApple,vis,{}); int cnt=0; for(int i=0;i<n;i++){ if(res[i])++cnt; } return cnt*2; } };
dfs+记忆化搜索
memo记录特定矩阵,切分k块的方案数
我用的是上下左右加k连成的字符串作为唯一状态标记
class Solution { public: const int mm=1e9+7; unordered_map<string,int> memo; bool check(vector<string>& pizza,int up,int left, int down,int right){ for(int i=up;i<=down;i++){ for(int j=left;j<=right;j++){ if(pizza[i][j]==‘A‘)return true; } } return false; } int func(vector<string>& pizza, int rst, int cst, int red, int ced, int k){ if(rst>red||cst>ced)return 0; if(k==1){ for(int i=rst;i<=red;i++){ for(int j=cst;j<=ced;j++){ if(pizza[i][j]==‘A‘){ return 1; } } } return 0; } string tmp=""; tmp+=to_string(rst);tmp+=‘/‘; tmp+=to_string(cst);tmp+=‘/‘; tmp+=to_string(red);tmp+=‘/‘; tmp+=to_string(ced);tmp+=‘/‘; k==10?tmp+="10":tmp+=(‘0‘+k); if(memo.find(tmp)!=memo.end())return memo[tmp]; int sum=0; for(int i=rst;i<red;i++){ if(check(pizza,rst,cst,i,ced)){ sum+=func(pizza,i+1,cst,red,ced,k-1); sum%=mm; } } for(int i=cst;i<ced;i++){ if(check(pizza,rst,cst,red,i)){ sum+=func(pizza,rst,i+1,red,ced,k-1); sum%=mm; } } memo[tmp]=sum; return memo[tmp]; } int ways(vector<string>& pizza, int k) { return func(pizza,0,0,pizza.size()-1,pizza[0].length()-1,k); } };
标签:push containe 切分 ble 苹果 搜索 css ref ini
原文地址:https://www.cnblogs.com/Dancing-Fairy/p/12863501.html