第五章 UVA
来源:互联网 发布:矩阵论引论课后答案 编辑:程序博客网 时间:2024/06/16 08:55
Uva 12096
自己代码
#include <bits/stdc++.h>using namespace std;typedef set<int> Set;map<Set, int> mark;int cnt = 0;Set s[2010];int ID(Set x){ if(!mark.count(x)) { mark[x] = cnt++; s[mark[x]] = x; } return mark[x];}int main(){ int t; cin >> t; while( t--){ int n; cin >> n; cnt = 0; mark.clear(); stack<int> sta; for(int i = 0; i < 2000; i++) s[i].clear(); for(int i = 0; i < n; i++){ string quer; cin >> quer; if(quer[0] == 'P') sta.push(ID(Set())); else if(quer[0] == 'D') sta.push(sta.top()); else{ Set x1 = s[sta.top()]; sta.pop(); Set x2 = s[sta.top()]; sta.pop(); Set x; //printf(" x1 %d x2 %d\n", ID(x1), ID(x2)); //cout<<quer[0]<<endl; if(quer[0] == 'U') set_union(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.begin())); if(quer[0] == 'I') set_intersection(x1.begin(), x1.end(), x2.begin(), x2.end(), inserter(x, x.begin())); if(quer[0] == 'A') x = x2, x.insert(ID(x1)); sta.push(ID(x)); } printf("%d\n", s[sta.top()].size()); } printf("***\n"); } return 0;}
代码库:
#include <iostream> #include <set> #include <map> #include <stack> #include <vector> #include <algorithm> using namespace std; typedef set<int> Set; map<Set,int> IDcache; vector<Set> Setcache; int ID(Set x) { if(IDcache.count(x)) return IDcache[x]; Setcache.push_back(x); return IDcache[x]=Setcache.size()-1; } #define ALL(x) x.begin(),x.end() #define INS(x) inserter(x,x.begin()) stack<int> s; int main(void) { int n; cin>>n; while(n--) { int m; cin>>m; for(int i=0;i<m;i++) { string op; cin>>op; if(op[0]=='P') s.push(ID(Set())); else if(op[0]=='D') s.push(s.top()); else { Set x1=Setcache[s.top()];s.pop(); Set x2=Setcache[s.top()];s.pop(); Set x; if(op[0]=='U') set_union(ALL(x1),ALL(x2),INS(x)); if(op[0]=='I') set_intersection(ALL(x1),ALL(x2),INS(x)); if(op[0]=='A') {x=x2;x.insert(ID(x1));} s.push(ID(x)); } cout<<Setcache[s.top()].size()<<endl; } cout<<"***"<<endl; } return 0; }
uva 540
// UVa540 Team Queue// Rujia Liu#include<cstdio>#include<queue>#include<map>using namespace std;const int maxt = 1000 + 10;int main() { int t, kase = 0; while(scanf("%d", &t) == 1 && t) { printf("Scenario #%d\n", ++kase); // 记录所有人的团队编号 map<int, int> team; // team[x]表示编号为x的人所在的团队编号 for(int i = 0; i < t; i++) { int n, x; scanf("%d", &n); while(n--) { scanf("%d", &x); team[x] = i; } } // 模拟 queue<int> q, q2[maxt]; // q是团队的队列,而q2[i]是团队i成员的队列 for(;;) { int x; char cmd[10]; scanf("%s", cmd); if(cmd[0] == 'S') break; else if(cmd[0] == 'D') { int t = q.front(); printf("%d\n", q2[t].front()); q2[t].pop(); if(q2[t].empty()) q.pop(); // 团体t全体出队列 } else if(cmd[0] == 'E') { scanf("%d", &x); int t = team[x]; if(q2[t].empty()) q.push(t); // 团队t进入队列 q2[t].push(x); } } printf("\n"); } return 0;}
uva 136
// UVa136 Ugly Numbers// Rujia Liu#include<iostream>#include<vector>#include<queue>#include<set>using namespace std;typedef long long LL;const int coeff[3] = {2, 3, 5};int main() { priority_queue<LL, vector<LL>, greater<LL> > pq; set<LL> s; pq.push(1); s.insert(1); for(int i = 1; ; i++) { LL x = pq.top(); pq.pop(); if(i == 1500) { cout << "The 1500'th ugly number is " << x << ".\n"; break; } for(int j = 0; j < 3; j++) { LL x2 = x * coeff[j]; if(!s.count(x2)) { s.insert(x2); pq.push(x2); } } } return 0;}
uva 400
// UVa400 Unix ls// Rujia Liu#include<iostream>#include<string>#include<algorithm>using namespace std;const int maxcol = 60;const int maxn = 100 + 5;string filenames[maxn];// 输出字符串s,长度不足len时补字符extravoid print(const string& s, int len, char extra) { cout << s; for(int i = 0; i < len-s.length(); i++) cout << extra;}int main() { int n; while(cin >> n) { int M = 0; for(int i = 0; i < n; i++) { cin >> filenames[i]; M = max(M, (int)filenames[i].length()); } // 计算列数cols和行数rows int cols = (maxcol - M) / (M + 2) + 1, rows = (n - 1) / cols + 1; print("", 60, '-'); cout << "\n"; sort(filenames, filenames+n); for(int r = 0; r < rows; r++) { for(int c = 0; c < cols; c++) { int idx = c * rows + r; if(idx < n) print(filenames[idx], c == cols-1 ? M : M+2, ' '); } cout << "\n"; } } return 0;}
uva 1592
// UVa1592 Database// Rujia Liu// 本程序只是为了演示STL各种用法,效率较低。实践中一般用C字符串和哈希表来实现。#include<iostream>#include<cstdio>#include<vector>#include<string>#include<map>#include<sstream>using namespace std;typedef pair<int,int> PII;const int maxr = 10000 + 5;const int maxc = 10 + 5;int m, n, db[maxr][maxc], cnt;map<string, int> id;int ID(const string& s) { if(!id.count(s)) { id[s] = ++cnt; } return id[s];}void find() { for(int c1 = 0; c1 < m; c1++) for(int c2 = c1+1; c2 < m; c2++) { map<PII, int> d; for(int i = 0; i < n; i++) { PII p = make_pair(db[i][c1], db[i][c2]); if(d.count(p)) { printf("NO\n"); printf("%d %d\n", d[p]+1, i+1); printf("%d %d\n", c1+1, c2+1); return; } d[p] = i; } } printf("YES\n");}int main() { string s; while(getline(cin, s)) { stringstream ss(s); if(!(ss >> n >> m)) break; cnt = 0; id.clear(); for(int i = 0; i < n; i++) { getline(cin, s); int lastpos = -1; for(int j = 0; j < m; j++) { int p = s.find(',', lastpos+1); if(p == string::npos) p = s.length(); db[i][j] = ID(s.substr(lastpos+1, p - lastpos - 1)); lastpos = p; } } find(); } return 0;}
uva 207
// UVa207 PGA Tour Prize Money// Rujia Liu#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<algorithm>#include<cassert>using namespace std;#define REP(i,n) for(int i = 0; i < (n); i++)const int maxn = 144;const int n_cut = 70;struct Player { char name[25]; int amateur; int sc[4]; int sc36, sc72, dq; int rnds;} player[maxn];int n;double purse, p[n_cut];bool cmp1(const Player& p1, const Player& p2) { if(p1.sc36 < 0 && p2.sc36 < 0) return false; // equal if(p1.sc36 < 0) return false; // p2 smaller if(p2.sc36 < 0) return true; // p1 smaller return p1.sc36 < p2.sc36;}bool cmp2(const Player& p1, const Player& p2) { if(p1.dq && p2.dq) { if(p1.rnds != p2.rnds) return p2.rnds < p1.rnds; if(p1.sc72 != p2.sc72) return p1.sc72 < p2.sc72; return strcmp(p1.name, p2.name) < 0; } if(p1.dq) return false; if(p2.dq) return true; if(p1.sc72 != p2.sc72) return p1.sc72 < p2.sc72; return strcmp(p1.name, p2.name) < 0;}void print_result() { printf("Player Name Place RD1 RD2"); printf(" RD3 RD4 TOTAL Money Won\n"); printf("---------------------------------------"); printf("--------------------------------\n"); int i = 0, pos = 0; while(i < n) { if(player[i].dq) { printf("%s ",player[i].name); REP(j,player[i].rnds) printf("%-5d", player[i].sc[j]); REP(j,4-player[i].rnds) printf(" "); printf("DQ\n"); i++; continue; } int j = i; int m = 0; // number of tied players bool have_money = false; double tot = 0.0; // total pooled money while(j < n && player[i].sc72 == player[j].sc72) { if(!player[j].amateur) { m++; if(pos < n_cut) { have_money = true; // yeah! they still have money tot += p[pos++]; } } j++; } // print player [i,j) together because they have the same rank int rank = i + 1; // rank of all these m players double amount = purse * tot / m; // if m=0, amount will be nan but we don't use it in that case :) while(i < j) { printf("%s ", player[i].name); char t[5]; sprintf(t, "%d%c", rank, m > 1 && have_money && !player[i].amateur ? 'T' : ' '); printf("%-10s", t); REP(e,4) printf("%-5d", player[i].sc[e]); // with prize if(!player[i].amateur && have_money) { printf("%-10d", player[i].sc72); printf("$%9.2lf\n", amount / 100.0); } else printf("%d\n", player[i].sc72); i++; } }}int main() { int T; char s[40]; gets(s); sscanf(s,"%d",&T); while(T--) { gets(s); // empty line // prize gets(s); sscanf(s,"%lf", &purse); REP(i,n_cut) { gets(s); sscanf(s, "%lf", &p[i]); } // players gets(s); sscanf(s, "%d", &n); assert(n <= 144); REP(k,n) { // read a 32-character line gets(s); // player name strncpy(player[k].name, s, 20); player[k].name[20] = 0; player[k].amateur = 0; if(strchr(player[k].name, '*')) { player[k].amateur = 1; } // scores player[k].sc36 = player[k].sc72 = player[k].dq=0; memset(player[k].sc, -1, sizeof(player[k].sc)); REP(i,4) { // raw score char t[5]; REP(j,3) t[j] = s[20 + i*3 + j]; t[3] = '\0'; // parse if(!sscanf(t,"%d", &player[k].sc[i])) { // DQ! player[k].rnds = i; player[k].dq = -1; if(i < 2) player[k].sc36 = -1; break; // skip other rounds (filled with -1, initially) } else { player[k].sc72 += player[k].sc[i]; if(i < 2) player[k].sc36 += player[k].sc[i]; } } } // round 1 sort(player, player+n, cmp1); assert(player[n_cut-1].sc36 >= 0); for(int i = n_cut-1; i < n; i++) if(i == n-1 || player[i].sc36 != player[i+1].sc36) { n = i+1; break; } // round 2 sort(player, player+n, cmp2); // print result print_result(); if(T) printf("\n"); } return 0;}
uva 814
// UVa814 The Letter Carrier's Rounds// Rujia Liu#include<iostream>#include<string>#include<vector>#include<set>#include<map>using namespace std;void parse_address(const string& s, string& user, string& mta) { int k = s.find('@'); user = s.substr(0, k); mta = s.substr(k+1);}int main() { int k; string s, t, user1, mta1, user2, mta2; set<string> addr; // 输入所有MTA,转化为地址列表 while(cin >> s && s != "*") { cin >> s >> k; while(k--) { cin >> t; addr.insert(t + "@" + s); } } while(cin >> s && s != "*") { parse_address(s, user1, mta1); // 处理发件人地址 vector<string> mta; // 所有需要连接的mta,按照输入顺序 map<string, vector<string> > dest; // 每个mta需要发送的用户 set<string> vis; while(cin >> t && t != "*") { parse_address(t, user2, mta2); // 处理收件人地址 if(vis.count(t)) continue; // 重复的收件人 vis.insert(t); if(!dest.count(mta2)) { mta.push_back(mta2); dest[mta2] = vector<string>(); } dest[mta2].push_back(t); } getline(cin, t); // 把"*"这一行的回车吃掉 // 输入邮件正文 string data; while(getline(cin, t) && t[0] != '*') data += " " + t + "\n"; for(int i = 0; i < mta.size(); i++) { string mta2 = mta[i]; vector<string> users = dest[mta2]; cout << "Connection between " << mta1 << " and " << mta2 <<endl; cout << " HELO " << mta1 << "\n"; cout << " 250\n"; cout << " MAIL FROM:<" << s << ">\n"; cout << " 250\n"; bool ok = false; for(int i = 0; i < users.size(); i++) { cout << " RCPT TO:<" << users[i] << ">\n"; if(addr.count(users[i])) { ok = true; cout << " 250\n"; } else cout << " 550\n"; } if(ok) { cout << " DATA\n"; cout << " 354\n"; cout << data; cout << " .\n"; cout << " 250\n"; } cout << " QUIT\n"; cout << " 221\n"; } } return 0;}
uva 221
// UVa221 Urban Elevations// Rujia Liu#include<cstdio>#include<algorithm>using namespace std;const int maxn = 100 + 5;struct Building { int id; double x, y, w, d, h; bool operator < (const Building& rhs) const { return x < rhs.x || (x == rhs.x && y < rhs.y); }} b[maxn];int n;double x[maxn*2];bool cover(int i, double mx) { return b[i].x <= mx && b[i].x+b[i].w >= mx;}// 判断建筑物i在x=mx处否可见bool visible(int i, double mx) { if(!cover(i, mx)) return false; for(int k = 0; k < n; k++) if(b[k].y < b[i].y && b[k].h >= b[i].h && cover(k, mx)) return false; return true;}int main() { int kase = 0; while(scanf("%d", &n) == 1 && n) { for(int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf%lf", &b[i].x, &b[i].y, &b[i].w, &b[i].d, &b[i].h); x[i*2] = b[i].x; x[i*2+1] = b[i].x + b[i].w; b[i].id = i+1; } sort(b, b+n); sort(x, x+n*2); int m = unique(x, x+n*2) - x; // x坐标排序后去重,得到m个坐标 if(kase++) printf("\n"); printf("For map #%d, the visible buildings are numbered as follows:\n%d", kase, b[0].id); for(int i = 1; i < n; i++) { bool vis = false; for(int j = 0; j < m-1; j++) if(visible(i, (x[j] + x[j+1]) / 2)) { vis = true; break; } if(vis) printf(" %d", b[i].id); } printf("\n"); } return 0;}
阅读全文
0 0
- 第五章 UVA
- The Mad Numerologist UVA-10785 第五章
- Ananagrams-UVA 156 白书第五章
- 【UVA】【DP】【第五章】【第一题】 History Grading
- UVa第五章STL应用 习题((解题报告))详细!
- 经典第五章例5.1 UVa 10474 Where is the Marble?(排序与检索)
- 经典第五章例5.2 UVA 101 The Blocks Problem(vector的基础应用)
- 经典第五章 例 5-3 UVa 10815 Andy's First Dictionity (set的用法)
- 经典第五章 例 5-4 UVA 156 Ananagrams(反片语)【map的应用】
- 经典第五章 习题 5-4 UVA 10763 Foreign Ex(思维题目)
- 经典第五章 例 5-6 UVA 540 Team Queue(队列的简单应用)【queue】
- 第五章
- 第五章
- 第五章
- 第五章
- 第五章
- 第五章
- 第五章
- C++多线程——AfxBeginThread
- JSON.stringify()和JSON.parse()
- maven使用exec插件运行Java main方法,以下是3种不同的操作方式。
- 提交文件到远程仓库git
- 【福利来了】rbd image qos
- 第五章 UVA
- HDU6178-Monkeys
- Java三大器(过滤器,拦截器,监听器)之补充:定时器+监听器-demo:管理服务器session,销毁过时session
- maven导出项目中所有依赖jar包
- python学习(3)
- “Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.4.1:clean (default-clean) on..
- CAGD(计算机辅助几何设计)大作业
- 获取Android设备唯一标识
- 数据查询优化建议