关联式容器
A - 狼人杀
“狼人杀”的原型是上世纪八十年代末,莫斯科大学心理学系的迪米特里·达维多夫发明了一种警察与杀手互猜身份的游戏,也就是“杀人游戏”。“杀人游戏”从一开始就只有核心规则得到了确定,本身是一款开源游戏。1990年以后,“杀人游戏”逐渐传播到了欧洲,随后到了美国,在每个不同的国家和地区都根据各自文化背景“演化”出了新的内容。
大哲是一个忠实的狼人杀爱好者,而且是高手中的高手。由于她过于擅长各类套路,并且具备敏锐的直觉与极强的心理素质,甚至还是一个心理学博士,她的朋友逐渐失去了游戏体验。为了照顾她的朋友,她决定制定一种不靠计谋、不用推理,全靠强运的“杀法”:
游戏玩家围成一个圈,从指定的第一个人开始数数,数到第 m 个人时,那个人就会被处死。之后从被处死的人的后一个人开始数数,再数到第 m 个人处死......依此方法不断杀死玩家。假如说玩家有好人与坏人两种身份,每种身份各 n 人,试问如何安排这些人的座位,能使得在处死 n 人后,剩下的 n 人都是好人
Input
多组数据,每组数据输入:好人和坏人的人数n(<=32767)、步长m(<=32767);
Output
对于每一组数据,输出2n个大写字母,‘G’表示好人,‘B’表示坏人,50个字母为一行,不允许出现空白字符。相邻两组输出之间有一个空行隔开。
Sample
Inputcopy | Outputcopy |
---|---|
2 3 2 4 | GBBG BGGB |
代码
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int m,n;
vector<int> v;
int main(){
while(~scanf("%d%d",&n,&m)){
v.clear();
for(int i=0;i<2*n;i++){
v.push_back(i);
} //初始化
int pos = 0; //开始位置0; 当前位置pos
for(int i=0;i<n;i++){ //取余
pos=(pos+m-1)%v.size();
v.erase(v.begin() + pos); //删除坏人
}
int j=0;
for(int i=0;i<2*n;i++){
if (!(i%50)&&i) cout<<endl; //50字母为1行
if(v.size()>j&&i==v[j]){
j++; cout<<"G";
}
else cout<<"B";
}
cout<<endl<<endl;
}
return 0;
}
B - 情书
雄雄学长的智商高达180,但他有个憨憨的习惯——写英语的时候喜欢把每一个单词都倒过来写。现在他写了一封给女神的情书,但是因为他的坏习惯,女神看不懂他的信,来找你帮忙。你能帮雄雄学长,向女神翻译他的信吗?
Input
输入包含多组数据。 第一行输入一个正整数 T 表示总的行数。接下来是T行。
每行包含一些单词,每行最多1000个字符。
Output
对于每一行输入的每个单词,请你都把他倒过来,雄雄学长会谢谢你的哟。
注意,雄雄学长只会将每个由空格所分隔的单词倒过来写,但单词间的顺序不会变。
Sample Input
3 !hO yM .raed m'I .gnoixgnoix I evol .uoy
Sample Output
Oh! My dear. I'm xiongxiong. I love you.
代码
#include<iostream>
#include<cstring>
using namespace std;
int n;
int main(){
char c[1005];
cin>>n;
n++;
while(n--){
gets(c);
int l=strlen(c);
int j=0;
for(int i=0;i<=l;i++)
{
if(c[i]==' '||c[i]=='\0')
{
for(int k=i-1;k>=j;k--)//遇空格或字符
cout<<c[k];
if(c[i]!='\0')
printf(" ");
j=i;//记录i
}
}
}
cout<<endl;
return 0;
}
C - 简单计算器
读入一个只包含 +, -, *, / 的非负整数计算表达式,计算该表达式的值。
Input
测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字符,整数和运算符之间用一个空格分隔。没有非法表达式。当一行中只有0时输入结束,相应的结果不要输出。
Output
对每个测试用例输出1行,即该表达式的值,精确到小数点后2位。
Sample
Inputcopy | Outputcopy |
---|---|
1 + 2 4 + 2 * 5 - 7 / 11 0 | 3.00 13.36 |
代码
#include<iostream>
#include<string.h>
#include<sstream>
#include<stack>
#include<ctype.h>
using namespace std;
int main(){
string s;
while(getline(cin, s) && s != "0"){
//判断是否为0
stack<double> nums;
stringstream s1(s);
//用stringstream分割空格
while(s1 >> s){
if(isdigit(s[0])){
//判断当前是否数字
stringstream s2;
double num;
s2 << s;
s2 >> num;
nums.push(num);
//存入数字
}else{
if(s == "+" || s == "-"){
if(s == "-"){
//如果是减,读入后面的数字*-1存进栈里
double num;
s1 >> num;
nums.push(num * -1);
}
}else{
//如果是乘除,读入后面的数字运算后存进栈里
double num;
s1 >> num;
double num1 = nums.top();
nums.pop();
if(s == "*"){
nums.push(num1 * num);
}else{
nums.push(num1 / num);
}
}
}
}
//栈内只剩下+,将所有的数字加起来
double num = nums.top();
nums.pop();
while(nums.size()){
double num1 = nums.top();
nums.pop();
num = num + num1;
}
printf("%.2lf\n", num);
}
return 0;
}
D - 营救嘉然小姐
牛子爷的嘉然小姐被私生饭绑架了
牛子爷特别喜欢看嘉然小姐的直播,希望能把嘉然小姐抢回来。
牛子爷杀进了私生饭的窝点,私生饭出了一道难题给牛子爷,如果答不出来,那他们就要把嘉然小姐独占。
问题就在墙上:
每个问题的第一行都有一个整数 N(有N个命令),还有 "FIFO" 或 "FILO".("FIFO"代表 "First In First Out"(先进先出), 并且 "FILO" 代表 "First In Last Out"(先进后出)).
接下来 N 行, 每行都是"IN M" 或者 "OUT M", (M 代表一个整数).
得到的输出答案就是绑嘉然小姐的密码锁的密码。作为一个合格的嘉心糖,牛子爷一定会破出这个密码,继续看嘉然小姐的直播。
Input
包含多组输入
第一行包含一个整数,代表组数
接下来的输入如描述所述
Output
每得到一个 "OUT", 你必须根据 "FIFO" 或 "FILO"的原则,立刻输出一个整数, 或者如果还没有得到任何整数的话就输出 "None"
Sample Input
4 4 FIFO IN 1 IN 2 OUT OUT 4 FILO IN 1 IN 2 OUT OUT 5 FIFO IN 1 IN 2 OUT OUT OUT 5 FILO IN 1 IN 2 OUT IN 3 OUT
Sample Output
1 2 2 1 1 2 None 2 3
思路
利用queue先进后出 stack先进后出的特点,写两个容器存储
代码
#include<cstdio>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
char ch[100];
int main(void)
{
int n,b;
while(~scanf("%d",&n))
{
while(n--)
{
int a;
char str[100];
scanf("%d %s",&a,str);
if(strcmp(str,"FIFO")==0)//先进先出
{
queue<int>q;
for(int i=0;i<a;i++)
{
scanf("%s",ch);
if(strcmp(ch,"IN")==0) //进入
{
scanf("%d",&b);
q.push(b);//队列入栈添加数据
}
else//要求输出
{
if(!q.empty()) //队列不为空就出队列
{
printf("%d\n",q.front());//最早被存入队列的元素
q.pop();
}
else
printf("None\n");
}
}
}
else if(strcmp(str,"FILO")==0)//先进后出
{
stack<int>s;//设立一个栈储存
for(int i=0;i<a;i++)
{
scanf("%s",ch);
if(strcmp(ch,"IN")==0) //进入
{
scanf("%d",&b);
s.push(b);//入栈添加数据
}
else//要求输出
{
if(!s.empty()) //栈不为空就出栈
{
printf("%d\n",s.top());//出
s.pop();
}
else
printf("None\n");
}
}
}
}
}
return 0;
}
E - CPY
The National Intelligence Council of X Nation receives a piece of credible information that Nation Y will send spies to steal Nation X’s confidential paper. So the commander of The National Intelligence Council take measures immediately, he will investigate people who will come into NationX. At the same time, there are two List in the Commander’s hand, one is full of spies that Nation Y will send to Nation X, and the other one is full of spies that Nation X has sent to Nation Y before. There may be some overlaps of the two list. Because the spy may act two roles at the same time, which means that he may be the one that is sent from Nation X to Nation Y, we just call this type a “dual-spy”. So Nation Y may send “dual_spy” back to Nation X, and it is obvious now that it is good for Nation X, because “dual_spy” may bring back NationY’s confidential paper without worrying to be detention by NationY’s frontier So the commander decides to seize those that are sent by NationY, and let the ordinary people and the “dual_spy” in at the same time .So can you decide a list that should be caught by the Commander?
A:the list contains that will come to the NationX’s frontier.
B:the list contains spies that will be sent by Nation Y.
C:the list contains spies that were sent to NationY before.
Input
There are several test cases.
Each test case contains four parts, the first part contains 3 positive integers A, B, C, and A is the number which will come into the frontier. B is the number that will be sent by Nation Y, and C is the number that NationX has sent to NationY before.
The second part contains A strings, the name list of that will come into the frontier.
The second part contains B strings, the name list of that are sent by NationY.
The second part contains C strings, the name list of the “dual_spy”.
There will be a blank line after each test case.
There won’t be any repetitive names in a single list, if repetitive names appear in two lists, they mean the same people.
Output
Output the list that the commander should caught (in the appearance order of the lists B).if no one should be caught, then , you should output “No enemy spy”
Sample
Inputcopy | Outputcopy |
---|---|
8 4 3 Zhao Qian Sun Li Zhou Wu Zheng Wang Zhao Qian Sun Li Zhao Zhou Zheng 2 2 2 Zhao Qian Zhao Qian Zhao Qian | Qian Sun Li No enemy spy |
代码
#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;
string a[105], b[105], c[105];
int main(){
int x, y, z;
while(cin >> x >> y >> z){
for(int i = 1; i <= x; i++){
cin >> a[i];
}
for(int i = 1; i <= y; i++){
cin >> b[i];
}
for(int i = 1; i <= z; i++){
cin >> c[i];
}
int ok1 = 1;
for(int i = 1; i <= x; i++){
for(int j = 1; j <= y; j++){
if(a[i] == b[j]){//共有
int ok = 1;
for(int k = 1; k <= z; k++){
if(a[i] == c[k]){ //
ok = 0;
break;
}
}
if(ok){
cout << a[i] << " ";
ok1 = 0;
}
break;
}
}
}
if(ok1){
cout << "No enemy spy";
}
cout << "\n";
}
}
F - Web Navigation
你的课程设计是想去写个浏览器,简单的浏览器有四个操作
BACK:访问上一个界面
FORWORD:访问下一个界面
VISIT:访问一个新页面
QUIT:结束访问
当然,你可以利用两个栈来完成这几个操作 现正在访问http://www.acm.org/ ,给出数次操作,输出每次操作后正在访问的页面
Input
多次访问操作,以QUIT结束输入
Output
对每次操作,输出操作后正在访问的页面,若操作越界则输出"Ignored",并将页面停留在边界界面。
Sample Input
VISIT http://acm.ashland.edu/ VISIT http://acm.baylor.edu/acmicpc/ BACK BACK BACK FORWARD VISIT http://www.ibm.com/ BACK BACK FORWARD FORWARD FORWARD QUIT
Sample Output
http://acm.ashland.edu/ http://acm.baylor.edu/acmicpc/ http://acm.ashland.edu/ http://www.acm.org/ Ignored http://acm.ashland.edu/ http://www.ibm.com/ http://acm.ashland.edu/ http://www.acm.org/ http://acm.ashland.edu/ http://www.ibm.com/ Ignored
代码
#include<iostream>
#include<cstring>
#include<sstream>
#include<stack>
using namespace std;
int main(){
// BACK:访问上一个界面
// FORWORD:访问下一个界面
// VISIT:访问一个新页面
// QUIT:结束访问
stack<string> s1, s2;
s1.push("http://www.acm.org/");
string s, str;
//用两个栈来存,s1存前面的,s1的栈顶表示当前页面,s2存后面的
while(cin >> s && s != "QUIT"){
if(s == "VISIT"){
cin >> str;
s1.push(str);
//访问新页面,下一页要清空
while(s2.size()){
s2.pop();
}
cout << str << "\n";
}else if(s == "BACK"){
if(s1.size() == 1){
//如果s1的size为1就不能继续往前了
cout << "Ignored\n";
continue;
}
s2.push(s1.top());
s1.pop();
cout << s1.top() << "\n";
}else if(s == "FORWARD"){
if(s2.empty()){
//s2为空就不能下一页了
cout << "Ignored\n";
continue;
}
cout << s2.top() << "\n";
s1.push(s2.top());
s2.pop();
}
}
}
G - 小鱼的数字游戏
Description
小鱼最近被要求参加一个数字游戏,要求它把看到的一串数字 ai(长度不一定,以 0 结束),记住了然后反着念出来(表示结束的数字 0 就不要念出来了)。这对小鱼的那点记忆力来说实在是太难了,你也不想想小鱼的整个脑袋才多大,其中一部分还是好吃的肉!所以请你帮小鱼编程解决这个问题。
Input
一行内输入一串整数,以 0 结束,以空格间隔。
Output
一行内倒着输出这一串整数,以空格间隔。
Sample 1
Inputcopy | Outputcopy |
---|---|
3 65 23 5 34 1 30 0 | 30 1 34 5 23 65 3 |
Hint
数据规模与约定
对于 100%的数据,保证 0≤ai≤2^31−1,数字个数不超过 100。
代码
#include<iostream>
#include<stack>//STL stack的头文件
using namespace std;
stack<long long> a;
long long c;
int main(){
while(1){
cin>>c;
if(c==0) break;
a.push(c);
}
while(!a.empty()){
cout<<a.top()<<" ";
a.pop();
}
return 0;
}
H - Chat Room
给出字符串 s. 请判断通过删掉s中的0或多个字符后是否能获得字符串 "hello". 例如, "ahhellllloou"可以获得"hello", 但是"hlelo"不能获得.
Input
输入包含唯一的一行,一个长度在1到100之间的字符串 s
Output
如果可以获得"hello",输出"YES" , 否则输出"NO".
Examples
Input
ahhellllloou
Output
YES
Input
hlelo
Output
NO
代码
#include <iostream>
#include <cstring>
using namespace std;
double ans;
char v[6]={"hello"};
char s[120];
int main(){
cin>>s;
int l;
l=strlen(s);
int j=0;
for(int i=0;i<l;i++){
if(s[i]==v[j]){
j++;
}
}
if(j==5) cout<<"YES";
else cout<<"NO";
}
I - Doors and Keys
出题人喜欢玩英雄联盟。
然而这位出题人出题时已经神志不清了,他甚至想编一个英雄联盟的题目!
虚空之女卡莎已经获得了足够的攻击力、法强和攻速以进化她的技能,然而她惊讶的发现自己没有习得任何技能。马上就要打团了,她准备以最快的速度去习得技能并进化对应技能。因此,她准备只按六次键来升级。职业选手的键位总是与众不同,她的rbg键分别表示升级她的QWE技能,进化技能则用RBG来完成。但队友已经在点她了,这就导致,她有点手忙脚乱,随便顺序按了这6个键各一次。
她的按键顺序可以用一个长度为6的字符串表示,其中r,g,b分别表示她习得Q,W,E技能,R,G,B分别表示她进化Q,W,E技能,她必须先习得技能才能去进化对应的技能,因此,错误的顺序会导致她无法进化全部三个技能。
而你,只是一个恰巧路过的乐子人。请你根据卡莎的按键顺序,帮助卡莎判断她是否进化了全部的三个技能。
Input
第一行包含一个整数t (1≤t≤720) — 测试用例的数量.
每个测试用例由一个字符串组成。 每个字符是R, G, B (进化), r, g, b (习得), 中的一个,并且每个字符只出现一次。
Output
对于每个测试用例,如果卡莎进化了全部技能,则打印 YES。 否则,打印 NO。
Sample 1
Inputcopy | Outputcopy |
---|---|
4 rgbBRG RgbrBG bBrRgG rgRGBb | YES NO YES NO |
Note
在第一个测试用例中,卡莎首先习得所有技能,然后进化所有技能。
在第二个测试用例中,卡莎首先进化R(Q技能),但此时她还未习得该技能。
在第三个测试用例中,卡莎先升级技能再进化技能,重复三次。
代码
#include<iostream>
#include<cstring>
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
string s;
cin >> s;
if(s.find('r') < s.find("R") && s.find('g') < s.find("G") && s.find('b') < s.find("B")){
cout << "YES\n";
}else{
cout << "NO\n";
}
}
}
STL经典例题
A - shopping
Every girl likes shopping,so does dandelion.Now she finds the shop is increasing the price every day because the Spring Festival is coming .She is fond of a shop which is called "memory". Now she wants to know the rank of this shop's price after the change of everyday.
Input
One line contians a number n ( n<=10000),stands for the number of shops.
Then n lines ,each line contains a string (the length is short than 31 and only contains lowercase letters and capital letters.)stands for the name of the shop.
Then a line contians a number m (1<=m<=50),stands for the days .
Then m parts , every parts contians n lines , each line contians a number s and a string p ,stands for this day ,the shop p 's price has increased s.
Output
Contains m lines ,In the ith line print a number of the shop "memory" 's rank after the ith day. We define the rank as :If there are t shops' price is higher than the "memory" , than its rank is t+1.
Sample
Inputcopy | Outputcopy |
---|---|
3 memory kfc wind 2 49 memory 49 kfc 48 wind 80 kfc 85 wind 83 memory | 1 2 |
题目大意
有N家店,在M天里每天价格飞升,问在涨价后memory这家店的价格在N家店中排第几
代码
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <map>
using namespace std;
int main()
{
int n;
while(cin>>n){
string shop;
int day;//天数
for(int i=0;i<n;i++)
cin>>shop;//店名
cin>>day;
map<string,int> shops;
int zj; //价格变化
string s;
while(day--){
for(int i=0;i<n;i++){
cin>>zj>>s;
shops[s]+=zj;
}
int ans=1;
map<string,int>::iterator it;
for(it=shops.begin();it!=shops.end();it++){
if (it->second > shops["memory"]) ans++;
}
cout <<ans <<endl;
}
}
return 0;
}
B - Ignatius and Princess II
Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, "I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too." Ignatius says confidently, "OK, at last, I will save the Princess."
"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?
Input
The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub's demand. The input is terminated by the end of file.
Output
For each test case, you only have to output the sequence satisfied the BEelzebub's demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.
Sample
Inputcopy | Outputcopy |
---|---|
6 4 11 8 | 1 2 3 5 6 4 1 2 3 4 5 6 7 9 8 11 10 |
题目大意
求 1-N 排列的第 M 小的形式
代码
#include<iostream>
#include<algorithm>
using namespace std;
int a[1005];
int main(){
int n,m;
while(cin >> n >> m){
for(int i=1;i<=n;i++) a[i]=i;
int b=1;
do{
if(b==m) break;
b++;
}while(next_permutation(a+1,a+n+1));
for(int i=1;i<=n;i++){
if(i==1) cout << a[i];
else cout << ' ' <<a[i];
}
cout << '\n';
}
return 0;
}
C - 排列2
Ray又对数字的列产生了兴趣:
现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数。
Input
每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。
Output
对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。
每组输出数据间空一行,最后一组数据后面没有空行。
Sample
Inputcopy | Outputcopy |
---|---|
1 2 3 4 1 1 2 3 0 1 2 3 0 0 0 0 | 1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 1123 1132 1213 1231 1312 1321 2113 2131 2311 3112 3121 3211 1023 1032 1203 1230 1302 1320 2013 2031 2103 2130 2301 2310 3012 3021 3102 3120 3201 3210 |
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int a[4],ok=0;
cin>>a[0]>>a[1]>>a[2]>>a[3];
while(1){
if(a[0]+a[1]+a[2]+a[3]==0) break;
sort(a,a+4);//排序
int k=a[0];
if(a[0]!=0) cout<<a[0]<<a[1]<<a[2]<<a[3];
while(next_permutation(a,a+4)){
if(a[0]==k&&a[0]!=0) cout<<" "<<a[0]<<a[1]<<a[2]<<a[3];
else{
if(a[0]!=0){
if(k!=0) cout<<endl;//换行
cout<<a[0]<<a[1]<<a[2]<<a[3];
}
k=a[0];
}
}
cout<<endl;
cin>>a[0]>>a[1]>>a[2]>>a[3];//下次不退出才换行
if(a[0]+a[1]+a[2]+a[3]!=0) cout<<endl;
}
return 0;
}
D - Where is the Marable
Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them. Then Meena would ask Raju to find the first marble with a certain number. She would count 1...2...3. Raju gets one point for correct answer, and Meena gets the point if Raju fails. After some fixed number of trials the game ends and the player with maximum points wins. Today it’s your chance to play as Raju. Being the smart kid, you’d be taking the favor of a computer. But don’t underestimate Meena, she had written a program to keep track how much time you’re taking to give all the answers. So now you have to write a program, which will help you in your role as Raju.
Input
There can be multiple test cases. Total no of test cases is less than 65. Each test case consists begins with 2 integers: N the number of marbles and Q the number of queries Mina would make. The next N lines would contain the numbers written on the N marbles. These marble numbers will not come in any particular order. Following Q lines will have Q queries. Be assured, none of the input numbers are greater than 10000 and none of them are negative. Input is terminated by a test case where N = 0 and Q = 0.
Output
For each test case output the serial number of the case. For each of the queries, print one line of output. The format of this line will depend upon whether or not the query number is written upon any of the marbles. The two different formats are described below: • ‘x found at y’, if the first marble with number x was found at position y. Positions are numbered 1, 2, . . . , N. • ‘x not found’, if the marble with number x is not present. Look at the output for sample input for details.
Sample Input
4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0
Sample Output
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3
hint
排序输入的N个数字,输出M个给出的数在其中的位置,数字不在其中则输出not found
#include<iostream>
#include<algorithm>//sort,lower_bound头文件
using namespace std;
int cnt[100000];
int main(){
int n,m,count=1;//标记case
while(cin >> n >> m){
if(!n&&!m) break;
printf("CASE# %d:\n",count++);
for(int i=1;i<=n;i++) cin >> cnt[i]; //位置
sort(cnt+1,cnt+n+1);//关键排序
while(m--){
int a;
cin >> a;
int b=lower_bound(cnt+1,cnt+n+1,a)-cnt; //编号
//lower_bound用于查找指定区域内查找不小于目标值的第一个元素
if(cnt[b]==a) printf("%d found at %d\n",a,b);
else printf("%d not found\n",a);
}
}
return 0;
}
E - 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
whe
#include<set>
#include<iostream>
#include<string>
#include<sstream>
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]);
//tolower将大写字母转为小写
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;
return 0;
}
F - Ugly Numbers
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, ...
shows the first 11 ugly numbers. By convention, 1 is included. Write a program to find and print the 1500’th ugly number.
Input
There is no input to this program.
Output
Output should consist of a single line as shown below, with ‘’ replaced by the number computed. Sample Output
The 1500'th ugly number is < number >
#include <iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
const int c[3]={2,3,5};
int main()
{
priority_queue<ll,vector<ll>,greater<ll> > pq;// 优先队列
set<ll> s;//集合标记某个数字是否出现过
pq.push(1);//记录1为第一个ugly number
s.insert(1);
for(int i=1;;i++){
ll x=pq.top();pq.pop();
if(i==1500){
cout<<"The 1500'th ugly number is "<<x<<'.'<<endl;
break;
}
for(int j=0;j<3;j++){
ll x2=x*c[j];
if(!s.count(x2)){//没有出现过
s.insert(x2);pq.push(x2);//标记出现过,插入队列
}
}
}
return 0;
}
G - 单词数
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
Sample
Inputcopy | Outputcopy |
---|---|
you are my friend # | 4 |
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
int main(){
string s;
set<string> se;
set<string>::iterator it;
while(getline(cin,s)&&s!="#"){
stringstream ss;
ss<<s;
string p;
while(ss>>p){
se.insert(p);
}
cout << se.size() << endl;
se.clear();
}
return 0;
}
H - 士兵队列训练问题
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
Input
本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
Output
共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
Sample
Inputcopy | Outputcopy |
---|---|
2 20 40 | 1 7 19 1 19 37 |
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
int t;
cin >> t;
while(t--){
vector<int> v;
int n;
cin >> n;
for(int i=1;i<=n;i++) v.push_back(i); //记录编号
int add=1;
while(v.size()>3){
if(add==1){ //1212,2出列
for(int i=1;i<v.size();i++){
v.erase(v.begin()+i,v.begin()+i+1);
}
add=2;
}
else{ //123123,3出列
for(int i=2;i<v.size();i+=2){
v.erase(v.begin()+i,v.begin()+i+1);
}
add=1;
}
}
for(int i=0;i<v.size();i++){
if(!i) cout << v[i];
else cout << ' ' << v[i];
}
cout << endl;
}
return 0;
}
I - 看病要排队
优先级队列friend bool operator http://t.csdn.cn/jrPYK
看病要排队这个是地球人都知道的常识。
不过经过细心的0068的观察,他发现了医院里排队还是有讲究的。0068所去的医院有三个医生(汗,这么少)同时看病。而看病的人病情有轻重,所以不能根据简单的先来先服务的原则。所以医院对每种病情规定了10种不同的优先级。级别为10的优先权最高,级别为1的优先权最低。医生在看病时,则会在他的队伍里面选择一个优先权最高的人进行诊治。如果遇到两个优先权一样的病人的话,则选择最早来排队的病人。
现在就请你帮助医院模拟这个看病过程。
Input
输入数据包含多组测试,请处理到文件结束。
每组数据第一行有一个正整数N(0<N<2000)表示发生事件的数目。
接下来有N行分别表示发生的事件。
一共有两种事件:
1:"IN A B",表示有一个拥有优先级B的病人要求医生A诊治。(0<A<=3,0<B<=10)
2:"OUT A",表示医生A进行了一次诊治,诊治完毕后,病人出院。(0<A<=3)
Output
对于每个"OUT A"事件,请在一行里面输出被诊治人的编号ID。如果该事件时无病人需要诊治,则输出"EMPTY"。
诊治人的编号ID的定义为:在一组测试中,"IN A B"事件发生第K次时,进来的病人ID即为K。从1开始编号。
Sample
Inputcopy | Outputcopy |
---|---|
7 IN 1 1 IN 1 2 OUT 1 OUT 2 IN 2 1 OUT 2 OUT 1 2 IN 1 1 OUT 1 | 2 EMPTY 3 1 1 |
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct node{
int Dj,id;//优先等级和先后顺序
friend bool operator<(node a,node b){
if(a.Dj==b.Dj)
return a.id>b.id;
return a.Dj<b.Dj;
}
};
int main()
{
node temp;
char str[5];
int n=0;
while(~scanf("%d",&n)) {
priority_queue<node> q[4];
temp.id=0;
while(n--){
scanf("%s",str);
if(str[0]=='I'){
temp.id++;
int doc,dj;//医生,病人等级
scanf("%d%d",&doc,&dj);
temp.Dj=dj;
q[doc].push(temp);
}
else{
int doc;
scanf("%d",&doc);
if(q[doc].empty()){
cout<<"EMPTY"<<endl;
}
else{
cout<<q[doc].top().id<<endl;
q[doc].pop();//出院
}
}
}
}
return 0;
}
J - Rveisible
Problem Statement
There are N sticks with several balls stuck onto them. Each ball has a lowercase English letter written on it.
For each i=1,2,…,N, the letters written on the balls stuck onto the i-th stick are represented by a string Si. Specifically, the number of balls stuck onto the i-th stick is the length ∣Si∣ of the string Si, andSi is the sequence of letters on the balls starting from one end of the stick.
Two sticks are considered the same when the sequence of letters on the balls starting from one end of one stick is equal to the sequence of letters starting from one end of the other stick. More formally, for integers i and j between 11 and N, inclusive, the i-th and j-th sticks are considered the same if and only if Si equals Sj or its reversal.
Print the number of different sticks among the N sticks.
Constraints
- N is an integer.
- 2≤N≤2×105
- Si is a string consisting of lowercase English letters.
- ∣Si∣≥1
- ∑i=1N∣Si∣≤2×10^5
Input
The input is given from Standard Input in the following format:
N S1 S2 ⋮⋮ SN
Output
Print the answer.
Sample 1
Inputcopy | Outputcopy |
---|---|
6 a abc de cba de abc | 3 |
- S2 =
abc
equals the reversal of S4 =cba
, so the second and fourth sticks are considered the same. - S2 =
abc
equals S6 =abc
, so the second and sixth sticks are considered the same. - S3 =
de
equals S5 =de
, so the third and fifth sticks are considered the same.
Therefore, there are three different sticks among the six: the first, second (same as the fourth and sixth), and third (same as the fifth).
#include <set>
#include<iostream>
#include <algorithm>
using namespace std;
int main(void)
{
int n;
cin >> n;
int ans = 0;
set<string> T;
string s;
for(int i = 1; i <= n; i++){
cin >> s; //输入字符串
if(T.count(s) == 0) ans++; //没出现过则计数
T.insert(s); //记录字符串
reverse(s.begin(), s.end()); //翻转字符串
T.insert(s); //记录翻转后的字符串
}
cout << ans << endl;
return 0;
}