C:
*可以保留删除或者增加
? 保留或者删除
#include<bits/stdc++.h> using namespace std; int main(){ string s; int k,len; cin>>s>>k; int sumx = 0; len = s.size(); for(int j=0;j<len;j++){ if(isalpha(s[j])) sumx++; else sumx--; } if(sumx>k){ cout<<"Impossible"<<endl; }else{ string ans = ""; int sum = k-sumx; for(int j=0;j<len-1;j++){ if(isalpha(s[j])&&isalpha(s[j+1])){ ans+=s[j]; }else{ if(s[j+1] == '?'){ if(sum){ sum--; ans+=s[j]; } }else if(s[j+1] == '*'){ while(sum){ sum--; ans+=s[j]; } } } } if(isalpha(s[len-1])){ ans+=s[len-1]; } if(ans.size()==k) cout<<ans<<endl; else{ cout<<"Impossible"<<endl; } } return 0; }
D:
把深度为偶数的节点隐藏掉(-1) 给出每个节点的父亲以及这个点到 根部的 value值总和 包括自己在内 求这棵树最小的value总和
#include<bits/stdc++.h> using namespace std; #define maxn 100005 #define inf 99999999999999 #define LL long long #define debug(x) cout<<x<<endl vector<LL>q[maxn]; LL a[maxn],ans[maxn],va=0; bool flag = 0; void dfs(LL u,LL fa,LL deep){ if(deep%2==0){ LL mi = inf; for(int j=0;j<q[u].size();j++){ mi = min(mi,a[q[u][j]]); } if(mi==inf){ ans[u] = 0; a[u] = a[fa]; }else{ a[u] = mi; ans[u] = a[u] - a[fa]; } }else{ if(fa!=0){ ans[u] = a[u] - a[fa]; } } for(int j=0;j<q[u].size();j++){ int v = q[u][j]; dfs(v,u,deep+1); } } int main(){ LL n; scanf("%lld",&n); for(LL j=2;j<=n;j++){ LL x; scanf("%d",&x); q[x].push_back(j); } for(LL j=1;j<=n;j++){ scanf("%lld",&a[j]); } ans[1]=a[1]; dfs(1,0,1); for(int j=1;j<=n;j++){ va+=ans[j]; //cout<<ans[j]<<endl; if(ans[j]<0){ cout<<"-1"<<endl; return 0; } } cout<<va<<endl; }