例题

例题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. ajax的两个重要参数contentType 和dataType

    contentType 是入参!!!!!! 是传递给后端参数的格式: contentType : 'application/json;charset=UTF-8', contentType : 'te ...

  2. scrapy框架基于CrawlSpider的全站数据爬取

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

  3. linux安装redis 完整步骤

    原文连接:https://www.cnblogs.com/lauhp/p/8487029.html 安装: 1.获取redis资源 wget http://download.redis.io/rele ...

  4. django中@property装饰器的运用

    python提供的内置装饰器——staticmethod.classmethod和property 在OSQA中,@property的使用频率是非常高的.下面就是它的使用方法: @property 可 ...

  5. 06. 用css实现三角形

    用css实现三角形   <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  6. 【实战分享】安卓app测试的一些记录

    一.app代码未混淆1.使用7zip解压apk文件2.执行命令:dex2jar.bat apk解压后文件夹中的classes.dex文件3.上述命令执行后会在apk文件夹中生成java源码文件clas ...

  7. 2017 FVDI2 ABRITES Commander with 18 Softwares FULL Version + FLY OBD Terminator + J2534 DrewTech Softwares

    Highlights of FVDI2 Abrites Commander Full Version: 1.Free update online. 2.This is full version FVD ...

  8. 小y的质数

    题目链接:https://ac.nowcoder.com/acm/contest/634/C 链接:https://ac.nowcoder.com/acm/contest/634/C来源:牛客网 题目 ...

  9. 使用autoconf完成编译配置

    使用过开源C/C++项目的同学们都知道,标准的编译过程已经变成了简单的三部曲:configure/make/make install, 使用起来很方便,不像平时自己写代码,要手写一堆复杂的Makefi ...

  10. app唤起的完美解决方案,及阻止浏览器的默认弹窗行为

    https://stackoverflow.com/questions/10237031/how-to-open-a-native-ios-app-from-a-web-appvar frame = ...