寒假第二周训练——STL题目汇总
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions:3112 | Accepted: 1106 |
Description
- 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 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
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
#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
两个节点间可能有多条边,不过一条边的两端必然是不同的节点。
每个节点都有一个能量值。
现在我们要编写一个项目管理软件,这个软件呢有两个操作:
1.给某个项目的能量值加上一个特定值。
2.询问跟一个项目相邻的项目的能量值之和。(如果有多条边就算多次,比如a和b有2条边,那么询问a的时候b的权值算2次)。
然后对于每个测试数据,第一行有两个整数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标号。
#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
Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了.
每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成.
两组测试数据之间有一个空行.最后一组测试数据之后没有空行.
#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;
}
#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) 收藏 举报
浙公网安备 33010602011771号