赞
踩
记录金银铜奖牌数,逐个对比或者结构体都可以
#include<bits/stdc++.h> using namespace std; struct node{ int id; int a; int b; int c; bool operator <(const node &fk){ if(a!= fk.a) return a > fk.a; else if(b != fk.b) return b > fk.b; else return c > fk.c; } }; const int N = 1e3 + 7; node sb[3]; int n; void solve(){ cin >> n; sb[0].id = 0,sb[1].id = 1; for(int i = 0;i < n;i ++){ int idx; cin >> idx; int x; cin >> x; if(x == 1) sb[idx].a ++; else if(x == 2) sb[idx].b ++; else sb[idx].c ++; } sort(sb,sb + 2); if(sb[0].id == 0){ for(int i = 0;i < 2;i ++){ cout << sb[i].a << " " << sb[i].b << " " << sb[i].c; cout << endl; } } else{ for(int i = 1;i >= 0;i --){ cout << sb[i].a << " " << sb[i].b << " " << sb[i].c; cout << endl; } } if(sb[0].id == 0) cout << "The first win!"; else cout << "The second win!"; } int main(){ solve(); return 0; }
注意看会不会有多个组成方式,多个的依旧是D
#include<bits/stdc++.h> using namespace std; const int N = 1e2 + 7; int n,m; void solve(){ cin >> n >> m; map<string,string> mp; for(int i = 0;i < n;i ++){ string s; cin >> s; string x; cin >> x; mp[s] = x; } map<string,string> mp2; for(int i = 0;i < m;i ++){ string s; cin >> s; if(mp.find(s) != mp.end()){ cout << mp[s] << endl; continue; } int flag = 0; int sum = 0; for(int j = 1;j < s.size();j ++){ string a = s.substr(0,j); string b = s.substr(j,s.size() - j); if(mp.find(a) != mp.end() && mp.find(b) != mp.end()){ string c = a + b; string d = mp[a]; d += mp[b]; mp2[c] = d; flag = 1; sum ++; } } if(!flag) cout << "D" << endl; else{ if(sum > 1) cout << "D" << endl; else cout << mp2[s] << endl; } } } int main(){ solve(); return 0; }
首先带着 0 1 存一下单向图
然后对每种可能开始的情况跑一下bfs看看最短的路径
比如出现过“fk” bfs看看可不可以从 “fk 1” 到达 “fk 0” 或者 可不可以从 “fk 1” 到达 “fk 0”,这样即可以通过fk 0 = fk 1
bfs中记录路径
#include<bits/stdc++.h> using namespace std; const int N = 1e3 + 7; int n; map<string,vector<string>> g; map<string,string> pre; string ans1 = "-1",ans2 = "-1"; map<string,int> d; int mn = 1e9; int bfs(string s1,string s2){ pre.clear(); d.clear(); queue<string> q; q.push(s1); while(q.size()){ string t = q.front(); q.pop(); if(t == s2) return d[t]; for(auto x : g[t]){ if(d.find(x) != d.end()) continue; pre[x] = t; d[x] = d[t] + 1; q.push(x); } } return -1; } void solve(){ cin >> n; map<string,int> mp; for(int i = 0;i < n;i ++){ string s1,s2; cin >> s1 >> s2; string s3,s4; cin >> s3 >> s4; string a = s1 + " " + s2,b = s3 + " " + s4; g[a].push_back(b); mp[s1] = 1,mp[s3] = 1; } for(auto x : mp){ string s1 = x.first + " 0"; string s2 = x.first + " 1"; int sb = bfs(s1,s2); if(sb != -1 && sb < mn){ mn = sb; ans1 = s1,ans2 = s2; } sb = bfs(s2,s1); if(sb != -1 && sb < mn){ mn = sb; ans1 = s2,ans2 = s1; } } int dig = bfs(ans1,ans2); vector<string> v; string cans2 = ans2; while(cans2 != ans1){ v.push_back(cans2); cans2 = pre[cans2]; } v.push_back(ans1); reverse(v.begin(),v.end()); for(int i = 0;i < v.size() - 1;i ++){ cout << v[i] << " "; if(i != v.size() - 1) cout << v[i + 1] << " "; } cout << "= " << ans1 << " " << ans2; } int main(){ solve(); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。