例题

例题5-1 大理石在哪儿(Where is the Marble?,Uva 10474)

主要是熟悉一下sort和lower_bound的用法

关于lower_bound:

http://blog.csdn.net/niushuai666/article/details/6734403

此外还有upper_bound

http://blog.csdn.net/niushuai666/article/details/6734650

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <cctype>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long int a[]; int main()
{
int n,q,i,x,cnt=;
while(~sf("%d%d",&n,&q) && n)
{
pf("CASE# %d:\n",cnt++);
for(i=;i<n;i++) sf("%d",&a[i]);
sort(a,a+n);
while(q--)
{
sf("%d",&x);
int p = lower_bound(a,a+n,x)-a;
if(a[p]!=x)
{
pf("%d not found\n",x);
continue;
}
else
pf("%d found at %d\n",x,p+);
}
}
}

例题5-2 木块问题(The Blocks Problem,Uva 101)

主要是熟悉vector的pb和resize,以及字符串结束的处理方法

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <cctype>
#include <vector>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long vector<int> pile[];
int n; void find_block(int x,int &p,int &h)
{
for(p=;p<n;p++)
{
for(h=;h<pile[p].size();h++)
{
if(pile[p][h] == x) return;
}
}
} void clear_block(int p,int h)
{
int i;
for(i = h+;i<pile[p].size();i++)
{
int b = pile[p][i];
pile[b].pb(b);
}
pile[p].resize(h+);
} void onto_block(int p,int h,int p2)
{
int i;
for(i = h;i<pile[p].size();i++)
pile[p2].pb(pile[p][i]);
pile[p].resize(h);
} void print()
{
int p,h;
for(p=;p<n;p++)
{
pf("%d:",p);
for(h=;h<pile[p].size();h++)
{
pf(" %d",pile[p][h]);
}
blank;
}
} int main()
{
int i,a,b;
sf("%d",&n);
for(i=;i<n;i++) pile[i].pb(i);
char s[],s1[]; while(sf("%s",s) && s[]!='q')
{ sf("%d%s%d",&a,s1,&b);
int pa,pb,ha,hb;
find_block(a,pa,ha);
find_block(b,pb,hb);
if(pa==pb) continue;
if(!strcmp(s,"move")) clear_block(pa,ha);
if(!strcmp(s1,"onto")) clear_block(pb,hb);
onto_block(pa,ha,pb);
}
print();
}

例题5-3 安迪的第一个字典(Andy's First Dictionary,Uva 10815)

主要是熟悉set的insert还有iterator

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <cctype>
#include <vector>
#include <set>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long set<string> dict; int main()
{
string s,buf;
while(cin>>s)
{
int i;
for(i=;i<s.length();i++)
{
if(isalpha(s[i])) s[i] = tolower(s[i]);
else s[i] = ' ';
}
stringstream ss(s);
while(ss>>buf) dict.insert(buf);
}
for(set<string>::iterator it = dict.begin();it!=dict.end();++it)
cout<<*it<<endl;
}

例题5-4 反片语(Ananagrams,Uva 156)

主要是熟悉map的用法。

给string和vector<string>排序可以用sort(str.begin(),str.end());即字典序

map<string,int> mp可以直接用mp[string] = int;在这道题可以表示某个字符串出现的次数

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <cctype>
#include <vector>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long map<string,int> cnt;
vector<string> words; string repr(const string &s)
{
string ans = s;
for(int i=;i<ans.length();i++)
{
ans[i] = tolower(ans[i]);
}
sort(ans.begin(),ans.end());
return ans;
} int main()
{
string s;
while(cin>>s)
{
if(s[]=='#') break;
words.pb(s);
string r = repr(s);
if(!cnt.count(r)) cnt[r] = ;
cnt[r]++;
}
vector<string> ans;
for(int i = ;i<words.size();i++)
{
if(cnt[repr(words[i])]==) ans.pb(words[i]);
}
sort(ans.begin(),ans.end());
for(int i = ;i<ans.size();i++)
{
cout<<ans[i]<<endl;
} }

例题5-5 集合栈计算机(The Set Stack Computer,ACM/ICPC NWERC2006,UVa12096)

map映射+vector,inserter插入迭代器的使用,set_union和set_intersection

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) typedef set<int> Set;
map<Set,int> IDcache;
vector<Set> Setcache; int ID(Set x)
{
if(IDcache.count(x)) return IDcache[x];
Setcache.pb(x);
return IDcache[x] = Setcache.size() - ;
} int main()
{
stack<int> s;
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
while(n--)
{
string op;
cin>>op;
if(op[]=='P') s.push(ID(Set()));
else if(op[]=='D') s.push(s.top());
else
{
Set x1 = Setcache[s.top()];s.pop();
Set x2 = Setcache[s.top()];s.pop();
Set x;
if(op[]=='A')
{
x = x2;
x.insert(ID(x1));
}
if(op[]=='U') set_union(ALL(x1),ALL(x2),INS(x));
if(op[]=='I') set_intersection(ALL(x1),ALL(x2),INS(x));
s.push(ID(x));
}
cout<<Setcache[s.top()].size()<<endl;
}
cout<<"***"<<endl;
} }

例题5-6 团体队列(Team Queue,UVa540)

队列的使用

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) int main()
{
int t,x,i,kase=;
while(sf("%d",&t),t)
{
pf("Scenario #%d\n",kase++);
map<int,int> team;
for(i=;i<t;i++)
{
int n;
sf("%d",&n);
while(n--)
{
sf("%d",&x);
team[x] = i;
}
}
queue<int> q,q2[];
string op;
while(cin>>op && op[]!='S')
{
if(op[] == 'E')
{
int r;
sf("%d",&r);
int y = team[r];
if(q2[y].empty()) q.push(y);
q2[y].push(r);
}
if(op[] == 'D')
{
int y = q.front();
pf("%d\n",q2[y].front());
q2[y].pop();
if(q2[y].empty())
q.pop();
}
}
blank;
}
}

例题5-7 丑数(Ugly Numbers,Uva 136)

优先队列的应用

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue int con[]={,,}; int main()
{
int i,j;
set<LL> s;
pqueue<LL,vector<LL>,greater<LL> >pq;
s.insert();
pq.push();
for(i=;;i++)
{
LL t = pq.top();pq.pop();
if(i==)
{
pf("The 1500'th ugly number is %I64d.\n",t);
break;
}
for(j=;j<;j++)
{
LL x = t*con[j];
if(!s.count(x))
{
s.insert(x);
pq.push(x);
}
}
}
}

例题5-8 Unixls命令(Unix ls,UVa400)

对于这样控制格式的要求(比如对齐)可以用规定长度然后填充的方式输出

col求出来后,row应该是(n-1)/col+1而不是n/col

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue const int maxcol = ;
string filenames[]; void print(const string& s,int len,char extra)
{
cout<<s;
for(int i = ;i<len-s.length();i++)
cout<<extra;
} int main()
{
int n,i,j;
while(~sf("%d",&n))
{
int ma = ;
for(i=;i<n;i++)
{
cin>>filenames[i];
ma = max(ma,(int)filenames[i].length());
}
int col = (maxcol-ma)/(ma+) + ;
int row = (n-)/col +;
print("",,'-');
blank;
sort(filenames,filenames+n);
for(i = ;i<row;i++)
{
for(j=;j<col;j++)
{
int idx = j*row + i;
if(idx<n) print(filenames[idx],j==col-?ma:ma+,' ');
}
blank;
}
}
}

例题5-9 数据库(Database,ACM/ICPC NEERC 2009,UVa1592)

这题的思路是:

因为字符串比较的话比较慢,所以可以先做一个预处理,将所有字符串用一个ID表示

读取字符是这样的:因为字符用,和回车分隔,所以可以用getchar每个字符读取,遇到,或\n就处理

处理字符可以用map映射,如果count==0就pb到ID集vector里

然后就是遍历。先用一个结构保存两个ID。三重循环遍历,如果相同的话则输出

struct里小于号的定义很不错,return x<r.x || x==r.x && y<r.y;先看第一个的大小,相等再看第二个

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue const int ROW = +;
const int COL = +; int m,n; struct node
{
int x,y;
node(int xx,int yy):x(xx),y(yy){}
bool operator <(const node& r) const { return x<r.x || x==r.x && y<r.y;}
}; map<string,int> IDcache;
vector<string> stringSet;
vector<int> Text[ROW];
map<node,int> data; int ID(string str)
{
//cout<<"str: "<<str<<endl;
if(IDcache.count(str)) return IDcache[str];
stringSet.pb(str);
//pf("id: %d\n",stringSet.size()-1);
return IDcache[str] = stringSet.size()-;
} void read()
{
string str;
int i,j;
char ch = getchar();
for(i=;i<n;i++)
{
for(;;)
{
ch = getchar();
if(ch=='\r' || ch == '\n')
{
if(!str.empty()) Text[i].pb(ID(str));
str.clear();
break;
}
if(ch!=',') str+=ch;
else
{
Text[i].pb(ID(str));str.clear();
}
}
// cout<<"i "<<i<<endl;
// for(j=0;j<Text[i].size();j++)
// cout<<"j "<<j<<" Text: "<<Text[i][j]<<endl;
} } void sol()
{
int c1,c2,r,i,j,k;
for(c1=;c1<m;c1++)
{
for(c2=c1+;c2<m;c2++)
{
data.clear();
for(r=;r<n;r++)
{
int x = Text[r][c1];
int y = Text[r][c2];
node p(x,y);
if(data.count(p))
{
pf("NO\n");
pf("%d %d\n%d %d\n",data[p]+,r+,c1+,c2+);
return;
}
else
data[p]= r;
}
}
}
pf("YES\n");
} int main()
{
int i,j;
while(~sf("%d%d",&n,&m))
{
read();
sol();
for(i=;i<n;i++) Text[i].clear();
IDcache.clear();
stringSet.clear();
}
}

例题5-10 PGA巡回赛的奖金(PGA Tour Prize Money,ACM/ICPC World Finals1990,UVa207)

例题5-11 邮件传输代理的交互(The Letter Carrier's Rounds, ACM/ICPC World Finals 1999, UVa814)

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue set<string> addr; void getn(string &s,string &user,string &mta)
{
int k = s.find('@');
user = s.substr(,k);
mta = s.substr(k+);
} int main()
{
string s,mta,user,ma,na,user1,mta1,t;
int n;
while(cin>>s && s[]!='*')
{
cin>>ma>>n;
while(n--)
{
cin>>na;
addr.insert(na+"@"+ma);
}
} while(cin>>s && s!="*")
{
getn(s,user,mta); vector<string> MTA;
map<string,vector<string> > dest;//用户
set<string> vis;
while(cin>>t && t!="*")
{
getn(t,user1,mta1);
if(vis.count(t)) continue;
vis.insert(t);
if(!dest.count(mta1))
{
MTA.pb(mta1);
dest[mta1] = vector<string>();
}
dest[mta1].pb(t);
}
string data;
getline(cin,t); while(getline(cin,t) && t[]!='*')
{
data+=" " + t +"\n";
} for(int i = ;i<MTA.size();i++)
{
string mta2 = MTA[i];
vector<string> users = dest[mta2];
cout<<"Connection between "<<mta<<" and "<<mta2<<endl;
cout<<" HELO "<<mta<<endl;
pf(" 250\n");
cout<<" MAIL FROM:<"<<s<<">\n";
pf(" 250\n");
bool ok = false;
for(int j = ;j<users.size();j++)
{
cout<<" RCPT TO:<"<<users[j]<<">"<<endl;
if(addr.count(users[j]))
{
ok = true;
pf(" 250\n");
}
else
pf(" 550\n");
}
if(ok)
{
cout<<" DATA\n 354\n"<<data;
pf(" .\n 250\n");
}
pf(" QUIT\n 221\n");
}
}
}

例题5-12 城市正视图(Urban Elevations, ACM/ICPC World Finals 1992, UVa221)

#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define INF 10000
#define MAXN 5010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue int n;
double s[*]; struct building
{
int id;
double x,y,w,h,d;
bool operator <(const building& a) const
{
return x<a.x || (x==a.x && y<a.y);
}
//一定要是小于号,不然会报错
}b[]; bool cover(int i,double mx)
{
return b[i].x<=mx && b[i].x+b[i].w>=mx;
} bool visible(int i,double mx)
{
if(!cover(i,mx)) return false;
for(int k = ;k<n;k++)
{
if(cover(k,mx) && b[k].y<b[i].y && b[k].h>=b[i].h)
return false;
}
return true;
} int main()
{
int kase = ;
while(sf("%d",&n)== && n)
{
for(int i = ;i<n;i++)
{
sf("%lf%lf%lf%lf%lf",&b[i].x,&b[i].y,&b[i].w,&b[i].d,&b[i].h);
s[i*]=b[i].x;
s[i*+]=b[i].x+b[i].w;
b[i].id=i+;
}
sort(b,b+n);
sort(s,s+n*);
int m = unique(s,s+n*)-s; if(kase++) blank;
pf("For map #%d, the visible buildings are numbered as follows:\n%d",kase,b[].id); for(int i=;i<n;i++)
{
bool vis = false;
for(int j = ;j<m-;j++)
if(visible(i,(s[j]+s[j+])/)){vis=true;break;}
if(vis){pf(" %d",b[i].id);};
}
blank; }
}

紫书第5章 C++STL的更多相关文章

  1. 紫书第五章训练2 F - Compound Words

    F - Compound Words You are to find all the two-word compound words in a dictionary. A two-word compo ...

  2. 紫书第三章训练1 D - Crossword Answers

    A crossword puzzle consists of a rectangular grid of black and white squares and two lists of defini ...

  3. 紫书第三章训练1 E - DNA Consensus String

    DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of ...

  4. 紫书第五章训练3 D - Throwing cards away I

    D - Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top ...

  5. 【紫书】(UVa12096) The SetStack Computer

    突然转进到第五章的low题目的原因是做到图论了(紫书),然后惊喜的发现第一题就做不出来.那么里面用到了这一题的思想,我们就先解决这题.当然,dp必须继续做下去,这是基本功.断不得. 题意分析 这条题真 ...

  6. UVa 1339,紫书P73,词频

    题目链接:https://uva.onlinejudge.org/external/13/1339.pdf 紫书P73 解题报告: #include <stdio.h> #include ...

  7. 正则表达式引擎的构建——基于编译原理DFA(龙书第三章)——3 计算4个函数

    整个引擎代码在github上,地址为:https://github.com/sun2043430/RegularExpression_Engine.git nullable, firstpos, la ...

  8. 学习GT一书前九章的体会

    学习Gilbarg和Trudinger一书前九章的体会 本书第二章,调和函数的基本性质进行展示.特别的对比较定理有深刻的阐述以及Perron方法的基本说明,并对Wiener准则作了简要说明. 第三章的 ...

  9. 【紫书】【重要】Abbott's Revenge UVA - 816 bfs 复杂模拟 带方向参数的迷宫

    题意:一个迷宫,每个交叉路口有一路标,限制了你从某方向进入该路口所能进入的路口. 题解:1.对于方向的处理:将node多增加一维dir,通过一个const 字符数组 加 上dir_id函数 以及一个方 ...

随机推荐

  1. [ 转 ] windows环境%变量%大全

    一.定义 环境变量一般是指在操作系统中用来指定操作系统运行环境的一些参数,比如临时文件夹位置和系统文件夹位置等.这点有点类似于DOS时期的默认路径,当你运行某些程序时除了在当前文件夹中寻找外,还会到设 ...

  2. string.Format("rspauth={0}",

    public string rspauth(string Username, string Realm, string Password, string Nonce, string Cnonce,   ...

  3. JQ的ready()方法与window.onload()的区别与联系

    JQ的ready()与window.onload()方法都是在文档加载完毕之后才会被触发的方法,但它们之间的区别也是很明显的. 1.区别与联系:   $(document).ready() windo ...

  4. Eclipse中的常见设置

    本文将移到下面的博客维护: 新的博客网址 当新建一个workspace时,习惯做下面的设置: 1. 在eclipse中,默认的Text file encoding是GBK(操作系统是中文简体):如果操 ...

  5. 基础篇:3.1)规范化:3d草绘

    本章目的:3d草绘不同于cad工程图,但也有自己的规范要求.草绘要多多练习. 1.建模草图绘制 草图是大多数 3D 模型的基础.通常,创建模型的第一步是绘制草图,随后可以从草图生成特征.将一个或多个特 ...

  6. 基础篇:3.3)规范化:3d装配图

    本章目的:规范化3d零件装配图,弄清楚装配层级划分,这也是机械的基本功夫. 1.装配通用原则 在装配建模设计中,应遵循以下通用原则:a)所有的装配单元应具有唯一性和稳定性,不允许冗余元素存在: //就 ...

  7. 【算法笔记】B1017 A除以B

    1017 A除以B (20 分)   本题要求计算 A/B ,其中 A 是不超过 1000 位的正整数,B 是 1 位正整数.你需要输出商数 Q 和余数 R,使得 A=B×Q+R 成立. 输入格式: ...

  8. squid的简单介绍

    squid的简单介绍 squid的概念 squid是一种用来缓存Internet数据的软件.接受来自人们需要下载的目标(object)的请求并适当的处理这些请求.也就是说,如果一个人想下载一web界面 ...

  9. 二分--POJ-3258

    POJ-3258,二分 题目 Description Every year the cows hold an event featuring a peculiar version of hopscot ...

  10. @RequestMapping 和 @RequestBody的区别

    @RequestMapping要求:application/x-www-form-urlencoded 或不填 @RequestBody要求: application/json