算法竞赛入门经典(第2版)-刘汝佳-第五章解题源码(C++语言)(部分)

例题5-1

本题思路:书中p108有详细思路。本题我用了vector进行排序和寻找相等元素。

#include
#include
#include
using namespace std;
int main()
{
	//freopen("datain.txt","r",stdin);
	//freopen("dataout.txt","w",stdout);
	int n,q,rnd=1;
	while(cin>>n>>q&&n)
	{
		cout<<"CASE# "< marble;
		while(n--)
		{
			int tmp;
			cin>>tmp;
			marble.push_back(tmp);	
		}
		sort(marble.begin(),marble.end());
		while(q--)
		{
			int tmp,index;
			cin>>tmp;
			index=lower_bound(marble.begin(),marble.end(),tmp)-marble.begin();
			if(marble[index]==tmp)
			cout<

例题5-2

本题主要学会通过resize的方法来进行元素的快速删除,并且学习连续cin string的方法来判断是否结束程序。

#include
#include
#include
using namespace std;
const int maxn=30;
int n;
vector pile[maxn];
void find_block(int a,int &p,int &h)
{
	for(p=0;p>n;
	string s1,s2;
	for(int i=0;i>s1>>a>>s2>>b)//接收输入的技巧要掌握 
	{
		int pa,pb,ha,hb;
		find_block(a,pa,ha);
		find_block(b,pb,hb);
		if(pa==pb) continue;
		if(s2=="onto") clear_above(pb,hb);
		if(s1=="move") clear_above(pa,ha);
		pile_onto(pa,ha,pb); 
	}
	print();
	return 0;
} 

例题5-3

cin string 是以空格为界的,本题通过stringstream来将有空格的string进行了分割。实际上,先将string转换为stringstream,输入到string中,在输入的时,本身string遇到空格就会停止。其次是对遍历set进行了了解。

#include
#include
#include
#include
using namespace std;
set dict;
int main()
{
	//freopen("datain.txt","r",stdin);
	//freopen("dataout.txt","w",stdout);
	string s,buf;
	while(cin>>s)
	{
		
		for(int i=0;i>buf)//通过字符串流,将字符串中根据空格进行了分离。 
			{
				dict.insert(buf);
			} 
			
	}
		for(set::iterator it =dict.begin();it!=dict.end();++it)
		cout<<*it<

例题5-4

本题目主要对单词进行排序,然后来判断是否为反片语。

#include
#include
#include
#include
#include
#include
using namespace std;

map cnt;
vector words;

string repr(const string &s)
{
	string ans=s;
	for(int i=0;i>s)
	{
		if(s[0]=='#') break;
		words.push_back(s);
		string r = repr(s);
		if(!cnt.count(r))
		{
			cnt[r]=0; 
		}	
			cnt[r]++;		
	}
	vector ans;
	for(int i=0;i

例题5-5

本题的重要思想是将索引和实体分离的策略,有助于操作,而集合的操作。使用了set_union函数,具体用法为

set_union(A.begin(),A.end(),B.begin(),B.end(),inserter(C1 , C1.begin() ) );前四个参数依次是第一的集合的头尾,第二个集合的头尾。第五个参数的意思是将集合A、B取合集后的结果存入集合C中。通过定义宏,进一步减少代码的重复性。另外注意set ()为空集。本题主要是拓宽了处理思路。有助于学习。

#include
#include
#include
#include
#include 
#include
#include   
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
using namespace std;

typedef set Set;
map IDcache;
vector Setcache; 

int ID (Set x)
{
	if(IDcache.count(x)) 
	{
	  return IDcache[x];
	} 
	Setcache.push_back(x);
	return IDcache[x] = Setcache.size()-1;
}
int main()
{
	//freopen("datain.txt","r",stdin);
	//freopen("dataout.txt","w",stdout);
	int rnd;
	cin>>rnd;
	while(rnd--)
	{
		stack s;
	int n;
	cin>>n;
	while(n--)
	{
		string op;
		cin>>op;
		if(op[0]=='P') 
		{
			s.push(ID(Set()));//s中存储的是集合的索引 
		} 
		
		else if(op[0]=='D') 
		{
			s.push(s.top());
		}
		else
		{
			Set x1 = Setcache[s.top()];//弹出索引,通过map找到集合。 
			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<

例题5-6

通过两个队列来模拟题中队列,通过map将两个队列进行连接。

#include
#include
#include
using namespace std;
const int maxt=1000+10;
int main()
{
	//freopen("datain.txt","r",stdin);
	//freopen("dataout.txt","w",stdout);
	int t,kase=1;
	while(cin>>t&&t)
	{
		cout<<"Scenario #"< team;
		for(int i=0;i>n;
			while(n--)
			{
				cin>>x;
				team[x]=i;
			}
			
		}
		queue q,q2[maxt];//q为团队队列,q2每个团队的队列 
		for(;;)
		{
			int x;
			string cmd;
			cin>>cmd;
			if(cmd[0]=='S') break;
			else if (cmd[0]=='D')
			{
				int t=q.front();
				cout<>x;
				int t=team[x];
				if(q2[t].empty())
				{
					q.push(t);
				}
				q2[t].push(x);
			}	
		}
		cout<

例题5-7

丑数的生成方法和优先队列的使用。

#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int coeff[3]={2,3,5}; 
int main()
{
	priority_queue,greater > pq;
	set 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 "<

大整数类(不完全)

struct BigInterger
{
	static const int BASE=100000000;//每个vector存储小于BASE的数 
	static const int WIDTH=8;//BASE 0 的个数。用于字符串输入的时候处理 
	vector s;
	BigInterger(long long num=0)//struct 中的构造函数 
	{
		*this = num;
	}
	BigInterger operator = (long long num)
	{
		s.clear();
		do
		{
			s.pushback(num%BASE);
			num=num/BASE;
		}while(num > 0);
		return *this;
	}
	BigInterger operator = (const string &str)
	{
		s.clear();
		int x ,len=(str.length()-1)/WIDTH+1;
		for(int i=0;i=0;i--)
		{
			char buf[20];
			sprintf(buf,"%08d",x.s[i]);//%8指的是WIDTH,将整形转化为字符型 
			for(int j=0;j> (istream &in,BigInterger &x)
	{
		string s;
		if(!(in>>s)) return in;
		x = s;//=号已经被重载 
		return in;	
	}
	

例题5-8

主要是格式控制。书中的代码更加简洁

#include
#include
#include
#include
#include
using namespace std;
const int maxn=100+20;
int main()
{
	//freopen("datain.txt","r",stdin);
	//freopen("dataout.txt","w",stdout);
	int n;
	while(cin>>n)
	{
		string input[maxn];
		int len=0,row=0,col=0;
		for(int i=0;i>input[i];			
			if(input[i].length()>len)
			{
				len=input[i].length();	
			}
		}
		col = (60-len)/(len+2)+1;
		row = ceil(double(n)/col); 	
		sort(input,input+n);
		for(int i=0;i<60;i++) 
		cout<<"-";
		cout<

例题5-9

本题主要注意通过逗号分割的读入方式,使用getline方法也可以,但是时间过长,建议使用getchar(),使用getchar()注意回收回车。另外一点是pair的使用。

#include
#include
#include
#include
#include
using namespace std;
const int maxn=10000+10;
const int maxm=10+10;
typedef pair Pair;

map IDcache;

map strint; 

 int ID(string &token)
 {
 	if(strint.count(token))
	 {
	 	return strint[token];	
	 } 
	 return strint[token]=strint.size()-1;
	
 } 
 
 int findpair(Pair p,int rindex)
 {
 	if(IDcache.count(p))
	 {
	 	return IDcache[p];
	 }
	 else
	 {
	 	IDcache[p]=rindex;
	 	return -1;
	 }
 	
 }
int main()
{
//	freopen("datain.txt","r",stdin);
	//freopen("dataout.txt","w",stdout);
	int n,m;
	
	while(cin>>n>>m)
	{
		int rindex=-1;
		int flag=0;
    	char ch;
    	string tmp[maxn][maxm];
        getchar();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                tmp[i][j] = "";
                while ((ch = getchar()) != ',' && ch != '\n')
                  tmp[i][j] += ch;//在原来的sting后面加入新的字符 
            }
        }

/*		vector tmp[maxn];//也可以通过getline读入,但是花费时间过长 
		for(int i=0;i>ss;
			stringstream sstr(ss);
			string token;	
			while(getline(sstr, token, ','))
			{
				tmp[i].push_back(token);
			}		
		} 	*/
	
		for(int p=0;p

习题5-1

本题目通过合适的方法对输入数据读取,在读取中,存储各个字符串的长度。使用迭代器对vector进行遍历。

#include
#include
#include
#include
#include
using namespace std;
void print(string s,int len)
{
	cout< words[1000];
	int len[1000][200];
	int maxlen[200];
	string s,buf;
	memset(len,0,sizeof(len));
	memset(maxlen,0,sizeof(maxlen));
	while(getline(cin,s))
	{
		stringstream ss(s);
		while(ss >> buf) 
		{
			words[i].push_back(buf);
			len[i][j++]=buf.length();	
		}
		i++;
		j=0;
		rows++;	
	}

	
	for(int j=0;j<200;j++)
	{
		int tmp=0;
		for(int i=0;itmp)
			{
				tmp=len[i][j];
			}
		}
		maxlen[j]=tmp;
		
	}
	
	for(int i=0;i::iterator iter=words[i].begin();iter!=words[i].end();iter++)
		{
			if(iter+1!=words[i].end())
			{
				 print(*iter,maxlen[j++]);
			}
			else
			{
				cout<<*iter;
			}
		  
		   
		}
		cout<

习题5-2

#include
using namespace std;
const int maxn=15;
void geDu(const int *v,int *tmp,int len)
{
	for (int i=0;i>n;
	while(n--)
	{
		int len,flag=0;
		cin>>len;
		int v[maxn];
		for(int i=0;i>v[i];
		}
		if(isall0(v,len))
		{
			cout<<"ZERO"<

习题5-3

队列的使用,注意q.pop()不会返回元素,只是表示出队列。使用q.front()来获得队前元素。

#include
#include
using namespace std;
int main()
{
	//freopen("datain.txt","r",stdin);
	//freopen("dataout.txt","w",stdout);
	int n;
	while(cin>>n&&n)
	{
		queue q;
		for(int i=1;i<=n;i++)
		{
			q.push(i);
		}
		cout<<"Discarded cards:";
		while(q.size()>1)
		{
			int tmp;
			cout<<" "<1)
			cout<<",";
			
		}
		cout<

习题5-4

本题使用pair和map,输入的时候,使用map记录<出发地,目的地>的个数。输入完成,只要<出发地,目的地>的个数等于<出发地,目的地>的个数,就输出YES,否则输出NO。

#include
#include
using namespace std;
typedef pair Pair;
int main()
{
	//freopen("datain.txt","r",stdin);
	//freopen("dataout.txt","w",stdout);
	int n;
	while(cin>>n&&n)
	{	
		int flag=1;
		map mp;
		while(n--)
		{
			int a,b;
			cin>>a>>b;
			
			Pair pr(a,b);
			if(!mp.count(pr))
			{
				mp[pr]=1;
			}
			else
			{
				mp[pr]++;	
			}
		}
			
		
		for(map::iterator iter=mp.begin();iter!=mp.end();iter++)
		{
			Pair a = iter->first;
		    Pair np(a.second,a.first);
		    if(mp[np]!=mp[a])
			{
				flag=0;
				break;
			}

		}
	
		if(flag)
		{
			cout<<"YES"<

习题5-5

本题目通过拆分单词串成两个子串,如果两个子串都在输入中,则说明该串为复合单词,使用substr()来取子串,使用lower_bound()进行查找,使用find()要超时,在已经排好序后,使用lower_bound更加节省时间。

#include
#include
#include
using namespace std;
int main()
{
	//freopen("datain.txt","r",stdin);
	//freopen("dataout.txt","w",stdout);
	string a;
	vector wd;
	while(cin>>a)
	{		
		wd.push_back(a);
	}
	for(vector::iterator iter=wd.begin();iter!=wd.end();iter++)
	{
		string a = *iter;
		//分解a;
		if(a.length()==1)
		{
			continue;
		}
		for(int i=1;i::iterator ita1,ita2;
			//使用find时间复杂度太高
			//ita1=find(wd.begin(),wd.end(),a1); 
			//ita2=find(wd.begin(),wd.end(),a2);
			 ita1= lower_bound(wd.begin(), wd.end(), a1);                                       
             ita2= lower_bound(wd.begin(), wd.end(), a2);                                                   
            if (*ita1 == a1 && *ita2 == a2)
			{
              cout << *iter << endl;
              break;
          	}
              
			
		}
	
	}
//	for(set::iterator iter=res.begin();iter!=res.end();iter++)
//	{
//		cout<<*iter<

习题5-6

本题我的代码不是很简洁。简单梳理下思路,先根据点的数量,确定对称轴的x坐标。在对称轴上的点,是对称。不在对称轴上的点需要找到一个关于这个对称轴对称的点。那么需要使用到排序,如果前面的一半的点都能找到他对应的点,那么就对称,否则不对称。

#include
#include
#include
#include
using namespace std;
struct Point 
{
	int x;
	int y;
};
bool operator == (const Point &a,const Point &b)
{
	if(a.x==b.x&&a.y==b.y)
		return true;
	else
		return false;
	
}

bool cmp(const Point &a,const Point &b)
{
	if(a.x>n;
	while(n--)
	{
		int m;
		vector vp;
		cin>>m;
		int z=m;
		while(z--)
		{
			int x,y;
			cin>>x>>y;
			struct Point p={x,y};
			vp.push_back(p);
		}
		sort(vp.begin(),vp.end(),cmp);
//		for(vector::iterator it=vp.begin();it!=vp.end();it++)
//			{
//				cout<x<<" ";
//				cout<y<::iterator it=vp.begin();it!=vp.end();it++)
			{
				if(*it==p1||p1.x==midx)
				{
					flag=1;
					break;
				}
			}
			if(!flag)
			{
				cout<<"NO"<::iterator it=vp.begin();it!=vp.end();it++)
			{
				if(*it==p1||p1.x==dc)
				{
					flag=1;
					break;
				}
			}
			if(!flag)
			{
				cout<<"NO"<

习题5-7

本题使用Queue,构建一个有优先级和标识是否为自己任务的结构体,根据题目要求一步一步进行即可。可以注意一下遍历队列的方法。

#include
#include
using namespace std;
typedef struct job
{
	int priority;
	int isself;
}Job;
int main()
{
	//freopen("datain.txt","r",stdin);
	//freopen("dataout.txt","w",stdout);
	int m;
	cin>>m;
	while(m--)
	{
		int qn,p;
		queue qujob;
		cin>>qn>>p;
		for(int i=0;i>pri;
			Job tj;
			tj.priority=pri;
			if(i==p)
			{
				tj.isself=1;
			}
			else
			{
				tj.isself=0;
			}
			qujob.push(tj);
		}
		int time=0;
		while(!qujob.empty())
		{	
			Job fj=qujob.front();
			qujob.pop();
			int flag=0;
			for(int i=0;ifj.priority)
				{
					flag=1;
				}
				qujob.pop();
				qujob.push(tmp);
			}
			if(flag)
			{
				qujob.push(fj);	
			}
			else
			{
				if(fj.isself==1)
				{
					time++;
					cout<

习题5-8

本题目主要是要审题对,一开始因为对题目没有理解透彻,出现一些错误。比如题目中没有告诉一开始书架上的书的摆放是按照输入顺序排序好了还是按照输入顺序。根据测试是摆放的书籍已经拍好序。又比如在shelve命令的时候,是按照还书的顺序放入暑假,还是先将还书进行排序后再放入书架。经过测试,是先将还书排序后,再依次放入书架。


#include
#include
#include
#include
#include
#include
using namespace std;
struct Book
{
	string title;
	string author;
	
};
bool operator < (const Book &a,const Book &b)
{
	if(a.author Bks;
	vector Sbks;
	set Rbks;
	while(getline(cin,s)&&s!="END")
	{
		string s1,s2;
		int k = s.find('"', 1);
        s1 = s.substr(0, k+1);
        s2 = s.substr(k+5);
		Book bk;
		bk.author=s2;	
		bk.title=s1;
		Bks[s1]=s2;
		Sbks.push_back(bk);	
	}
	sort(Sbks.begin(),Sbks.end(),cmp);
	while(getline(cin,s)&&s!="END")
	{
		string s1,s2;
		int k=s.find(' ', 1);	
        s1=s.substr(0, k);
        s2=s.substr(k+1);
		if(s1[0]=='R')
		{
			Book bk1;
			bk1.title=s2;
			bk1.author=Bks[s2];
			Rbks.insert(bk1);
					
		}
		else if (s1[0]=='B')
		{
			for(vector::iterator it=Sbks.begin();it!=Sbks.end();)
			{
				if(it->title==s2)
				{
					Sbks.erase(it);
					break;
				}
				else
				it++;
			}
				
		}
		else if (s1[0]=='S')
		{
			
			for(set::iterator it1=Rbks.begin();it1!=Rbks.end();it1++)
			{
				Book bk1;
				bk1=*it1;		
				vector::iterator it2 = lower_bound(Sbks.begin(),Sbks.end(),bk1);	
                cout << "Put " << it1->title;
                if (Sbks.empty() || it2 == Sbks.begin())
                  cout << " first" << endl;
                else
                  cout << " after " << (it2-1)->title << endl;
                  Sbks.insert(it2,bk1);				
			}
			cout<<"END"<








你可能感兴趣的:(算法竞赛)