寒假第二周训练——STL题目汇总

MANAGER
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions:3112   Accepted: 1106

Description

One of the programming paradigm in parallel processing is the producer/consumer paradigm that can be implemented using a system with a "manager" process and several "client" processes. The clients can be producers, consumers, etc. The manager keeps a trace of client processes. Each process is identified by its cost that is a strictly positive integer in the range 1 .. 10000. The number of processes with the same cost cannot exceed 10000. The queue is managed according to three types of requests, as follows: 
  • a x - add to the queue the process with the cost x; 
  • r - remove a process, if possible, from the queue according to the current manager policy; 
  • p i - enforce the policy i of the manager, where i is 1 or 2. The default manager policy is 1 
  • e - ends the list of requests. 

There are two manager policies: 
  • 1 - remove the minimum cost process 
  • 2 - remove the maximum cost process 

The manager will print the cost of a removed process only if the ordinal number of the removed process is in the removal list. 

Your job is to write a program that simulates the manager process. 

Input

The input is from the standard input. Each data set in the input has the following format: 
  • the maximum cost of the processes 
  • the length of the removal list 
  • the removal list - the list of ordinal numbers of the removed processes that will be displayed; for example 1 4 means that the cost of the first and fourth removed processes will be displayed
  • the list of requests each on a separate line. 

Each data set ends with an e request. The data sets are separated by empty lines.

Output

The program prints on standard output the cost of each process that is removed, provided that the ordinal number of the remove request is in the list and the queue is not empty at that moment. If the queue is empty the program prints -1. The results are printed on separate lines. An empty line separates the results of different data sets. 

An example is given in the following:

Sample Input

5
2
1 3
a 2
a 3
r
a 4
p 2
r
a 5
r
e

Sample Output

2
5

Source



题意:完成一个manager系统,包含四种指令,a x加入一个x,p x转化policy为x,r根据policy来删除,e结束指令。其中默认的policy为1,即删除最小的那个,policy2则是删除最大的那个。输入一个removal list,问你第i个被删除的元素是什么?并将其打印出来。


#include <iostream>
#include <cstdio>
#include <set>
//multiset的头文件还是set 
#include <vector>
using namespace std;
vector<int> q;
multiset<int> s;
//multiset可重复的set 
int policy,r_list[1000];
//创建removal list和policy
 
int main()
{
	int maxc,r_len,cost;
	//这个maxc好像并没有什么用,r_len为输入的removal list的长度 
	char ch;
	while(cin >> maxc >> r_len)
	{
		getchar();
		for (int i=0;i<r_len;i++)
			cin >> r_list[i];
		q.clear(); s.clear(); policy=1;
		//初始化,清空q,s,policy置1 
		while(cin >> ch && ch!='e')
		{
			if (ch=='a') {cin >> cost; getchar(); s.insert(cost);}
			else if (ch=='p') {cin >> policy; getchar();}
			else if (ch=='r')
			{
			    if(s.empty())  
					cout << "-1" << endl; 
    			else if(policy==1)
				{  
					multiset<int>::iterator it=s.begin();
					//迭代器,找到set的首位,即最小值 
        			q.push_back(*it); 
        			//将其值放入要输出的队列中 
        			s.erase(*it);
        			//删除最小值 
        		}
				else if(policy==2)
				{  
        			multiset<int>::reverse_iterator it=s.rbegin();
        			//逆向迭代器 
       				q.push_back(*it);
        			s.erase(*it);
    			}  
			}
		}
        for(int i=0;i<r_len;i++)  
        {  
			cout << q[r_list[i]-1] << endl;  
        }
        cout << endl;  
	}
	return 0;
}

                                        Andy's First Dictionary

Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.

You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like "Apple", "apple" or "APPLE" must be considered the same.

 

Input

The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.

 

Output

Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.

 

Sample Input

Adventures in Disneyland

Two blondes were going to Disneyland when they came to a fork in the
road. The sign read: "Disneyland Left."

So they went home.

 

Sample Output

a
adventures
blondes
came
disneyland
fork
going
home
in
left
read
road
sign
so
the
they
to
two
went
were
when

Source

UVA10815


题意:简单的讲就是将读到的单词放进字典。
#include <iostream>
#include <string>
#include <sstream>
//包含stringstream的头文件 
#include <set>
using namespace std;

set<string> dict;

int main()
{
	string s,buf;
	while (cin >> s)
	{
		for (int i=0;i<s.length();i++)
		{
			if (isalpha(s[i]))
				s[i]=tolower(s[i]);
			else
				s[i]=' ';
		}
		stringstream ss(s);
		//以流的形式读取字符串s 
		while(ss >> buf)
		dict.insert(buf);
		//将读出的单词放入字典 
	}
	set<string>::iterator it;
    for(it=dict.begin();it!=dict.end();it++)  
        cout << *it << endl;  
	return 0;
}

项目管理

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3370    Accepted Submission(s): 1265


Problem Description
我们建造了一个大项目!这个项目有n个节点,用很多边连接起来,并且这个项目是连通的!
两个节点间可能有多条边,不过一条边的两端必然是不同的节点。
每个节点都有一个能量值。

现在我们要编写一个项目管理软件,这个软件呢有两个操作:
1.给某个项目的能量值加上一个特定值。
2.询问跟一个项目相邻的项目的能量值之和。(如果有多条边就算多次,比如a和b有2条边,那么询问a的时候b的权值算2次)。
 

Input
第一行一个整数T(1 <= T <= 3),表示测试数据的个数。
然后对于每个测试数据,第一行有两个整数n(1 <= n <= 100000)和m(1 <= m <= n + 10),分别表示点数和边数。

然后m行,每行两个数a和b,表示a和b之间有一条边。
然后一个整数Q。

然后Q行,每行第一个数cmd表示操作类型。如果cmd为0,那么接下来两个数u v表示给项目u的能量值加上v(0 <= v <= 100)。
如果cmd为1,那么接下来一个数u表示询问u相邻的项目的能量值之和。

所有点从1到n标号。
 

Output
对每个询问,输出一行表示答案。
 

Sample Input
1 3 2 1 2 1 3 6 0 1 15 0 3 4 1 1 1 3 0 2 33 1 2
 

Sample Output
4 15 15
 

Author
CLJ
 

Source

题意:类似manager,相对更简单一点。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#define MAX_N 100005
using namespace std;
vector<int> G[MAX_N];

int main()
{
	int t,s[MAX_N]; cin >> t;
	while (t--)
	{
		memset(s,0,sizeof(s));
		int n,m,q,a,b,cmd,u,v,ans=0;
		cin >> n >> m;
		for (int i=0;i<m;i++)
		{
			scanf("%d%d",&a,&b);
			G[a].push_back(b);
			//单方向,有向图的创建 
			G[b].push_back(a);
			//无向图的创建 
		}
		cin >> q;
		for (int i=0;i<q;i++)
		{
			cin >> cmd;
			if (cmd==0) {scanf("%d%d",&u,&v); s[u]+=v;}
			else
			{
				ans=0;
				scanf("%d",&u);
				vector<int>::iterator it;
				//创建迭代变量it 
				for (it=G[u].begin();it!=G[u].end();it++)
				{
					ans+=s[*it];
				}
				printf("%d\n",ans);
			}
		}
		for (int i=1;i<=n;i++)
			G[i].clear();
		//勿忘清空图
		//memset(G,0,sizeof(G))是不对的!! 
	}
	return 0;
}

水果

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8723    Accepted Submission(s): 3463


Problem Description
夏天来了~~好开心啊,呵呵,好多好多水果~~
Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了.
 

Input
第一行正整数N(0<N<=10)表示有N组测试数据.
每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成.
 

Output
对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序.
两组测试数据之间有一个空行.最后一组测试数据之后没有空行.
 

Sample Input
1 5 apple shandong 3 pineapple guangdong 1 sugarcane guangdong 1 pineapple guangdong 3 pineapple guangdong 1
 

Sample Output
guangdong |----pineapple(5) |----sugarcane(1) shandong |----apple(3)
 

Source

题意:将水果按照产地,名称,个数记录成输出的格式(可以使用map嵌套map来实现)。

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
using namespace std;
struct place
{
	map<string,int> fruit;
}; 
 
int main()
{
	map<string,place> f;
	//在map中套用map 
	map<string,place>::iterator it; 
	map<string,int>::iterator it1;
	//创建两个迭代器 
	string fr,pl;
	int n,num,m; cin >> n;
	while (n--)
	{
		f.clear();
		cin >> m;
		while (m--)
		{
			cin >> fr >> pl >> num;
			f[pl].fruit[fr]+=num;
		}
		for (it=f.begin();it!=f.end();it++)
		{
			cout << it->first << endl;
			//it->first表示[]中的元素值,同理second表示对应的元素值 
			for (it1=it->second.fruit.begin();it1!=it->second.fruit.end();it1++)
				cout<<"   |----"<<it1->first<<"("<<it1->second<<")"<<endl;  
		}
		if(n) cout << endl;
	}
	return 0;
}
B. Misha and Changing Handles
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.

Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.

Input

The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.

Next q lines contain the descriptions of the requests, one per line.

Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20.

The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handle newis not used and has not been used by anyone.

Output

In the first line output the integer n — the number of users that changed their handles at least once.

In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old and new, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order.

Each user who changes the handle must occur exactly once in this description.

Examples
input
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
output
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123
 Source
题意:将A与B关联,若B与C关联,则A与C关联,输出最后的关联关系。
#include <iostream>
#include <cstdio>
#include <map>
using namespace std;

int main()
{
	int n;
	while (cin >> n)
	{
		string a,b;
		map<string,string> m;
		for (int i=0;i<n;i++)
		{
			cin >> a >> b;
			if (!m.count(a))
				m[a]=b;
			//若之前没有指向a的则令a指向自己 
			m[b]=m[a]; 
			//令原来指向a的指向b
			m.erase(a);
			//删除原来指向a的map 
		}
		map<string,string>::iterator it;
		cout << m.size() << endl;
		for (it=m.begin();it!=m.end();it++)
			cout << it->second << " " << it->first << endl; 
	}
	return 0;
}


 

posted on 2018-02-03 21:51  Radium_1209  阅读(135)  评论(0)    收藏  举报

导航