专题训练一

{A} + {B}

Problem Description
给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.

Input
每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.

Output
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.

Sample Input
1 2
1
2 3
1 2
1
1 2

Sample Output
1 2 3
1 2
题意
合并两个集合并且排序
分析
用vector存放两个集合的元素,先排序后去重

#include<iostream>
#include <stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
using namespace std;

vector<int> a;

int main(){
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		while(n--){
			int x;scanf("%d",&x);
			a.push_back(x);
		}
		while(m--){
			int x;scanf("%d",&x);
			a.push_back(x);
		}
		sort(a.begin(),a.end());
		a.erase(unique(a.begin(),a.end()),a.end());
		vector<int>::iterator it;
		for(it=a.begin();it!=a.end();++it)
		{
			if(it!=a.begin())cout<<" ";
			cout<<*it;
		}
		cout<<endl;
		a.clear();
	} 
	return 0;
} 

-----------------------------------------------------------------------------------------

火车进站问题

题目描述:
假设杭州东火车站只有一条铁路,并且所有火车都从一侧进来,从另一侧出去。那么,如果火车A先进站,然后火车B在火车A离开之前就进站,那么火车A直到火车B离开后才能离开,可参见下图。

现在,假设车站中有n(n<=9)列火车,所有火车都有一个ID(从1到n的编号),火车以O1的顺序进站,您的任务是确定火车是否可以按O2顺序出站。

Input
输入包含几个测试用例。

每个测试用例均包含三部分:一个表示火车数量的整数和两个字符串O1和O2,其中,火车的进站顺序用O1串表示,火车的出站顺序用O2串表示。

输入在文件末尾终止,更多信息参见样例。

Output
如果不能从O1的入站顺序得到O2的出站顺序,请输出字符串“ No.”。

如果能够得到,则请输出"Yes."
然后输出进站和出站的具体方式(“in”表示火车进站,“out”表示火车出站)。
在每个测试用例之后输出一行“ FINISH”。

更多信息参见样例。

Sample Input
3 123 321
3 123 312

Sample Output
Yes.
in
in
in
out
out
out
FINISH
No.
FINISH
题意
判断能否从给定的进站顺序得到出站顺序
分析
模拟进出站过程
代码

#include<iostream>
#include <stdio.h>
#include<vector>
using namespace std;

int stk[10],tt;
vector<string> a;

int main(){
	int n;char o1[10],o2[10];
	while(~scanf("%d%s%s",&n,o1,o2)){
		int k=0;
		for(int i=0;i<n;++i){
			stk[++tt]=o1[i]-'0';
			a.push_back("in");
			while(tt&&stk[tt]==o2[k]-'0'){
				k++;
				tt--;
				a.push_back("out");
			}
		}
		if(k==n){
			cout<<"Yes."<<endl;
			for(int i=0;i<n*2;++i)cout<<a[i]<<endl;
		}
		else cout<<"No."<<endl;
		cout<<"FINISH"<<endl;
		a.clear();tt=0;
	}
	return 0;
} 

-----------------------------------------------------------------------------------------

统计书名

题目描述:
嗷嗷嗷非常喜欢看书。每当他看完一本书,他就会用他的小本本记下书名。但嗷嗷嗷看书时太过忘我,以至于自己看过的书都会再看一遍并照样记录下来。当嗷嗷嗷回过神,想统计自己一共看了多少本不同的书,他把小本本交给了你,你能帮帮他吗?

Input
多组输入输出,请处理到输入结束。
每组数据,第一行有两个整数n (0<n<=100)代表书名的个数。
接下来有n行,每有一个个字符串s,代表书名(仅含大小写字母)

Output
每组数据输出一行,一个整数cnt,代表嗷嗷嗷看了不同的书的数量

Sample Input
5
aoaoao
aoao
acm
meow
aoaoao

Sample Output
4
题意
统计不重复的书名的个数
分析
用Trie树保存书名,每个书名只统计一次
代码

#include<iostream>
#include <stdio.h>
#include<string.h>
#include<vector>
using namespace std;
const int N=100005;
int son[N][52],cnt[N],idx,ans;
char str[N];
void insert(char str[]){
	int p=0;
	for(int i=0;str[i];++i)
	{
		int u;
		if(str[i]<='Z')u=str[i]-'A';
		else u=str[i]-'a'+26;
		if(!son[p][u])son[p][u]=++idx;
		p=son[p][u];
	}
	if(!cnt[p])cnt[p]++,ans++;
}
int main(){
	int n;
	while(~scanf("%d",&n)){
		memset(son,0,sizeof(son));
		memset(cnt,0,sizeof(cnt));
		idx=0;ans=0;
		while(n--)scanf("%s",str),insert(str);
		cout<<ans<<endl;
	}
	return 0;
} 

-----------------------------------------------------------------------------------------

第M个数列

题目描述:
给定1到N的序列,我们定义1,2,3 …N-1,N是由1到N组成的所有序列中的最小序列(每个数字只能使用一次)。则很容易看出,第二个最小的序列是1,2,3 … N,N-1。
现在,给定两个数字N和M,请告诉由数字1到N组成的第M个最小的序列是什么。

Input
输入包含多组测试数据,请处理到文件结束。
每组测试数据包含两个整数N和M(1<=N<=1000, 1<=M<=10000)。
数据保证一定有满足要求的数列。

Output
请输出满足要求的数列。数列的两个数之间用1个空格隔开,并且最后一个数后面没有空格。
每组数据输出一行。

Sample Input
6 4
11 8

Sample Output
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
题意
输出1到n按照字典序排列的第m小的序列
分析
next_permutation函数对数组按照下一个字典序对数组元素排列,从最小的字典序开始调用m-1次即得到第m小的序列
代码

#include<iostream>
#include <stdio.h>
#include<algorithm> 
using namespace std;
int a[1005];
int main(){
	int n,m;
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=0;i<n;++i)a[i]=i+1;
		for(int i=1;i<m;++i)next_permutation(a,a+n);
		for(int i=0;i<n;++i){
			if(i!=0)cout<<" ";
			cout<<a[i];
		}
		cout<<endl;
	}
	return 0;
} 

-----------------------------------------------------------------------------------------

What Are You Talking About

题目描述:
Ignatius is so lucky that he met a Martian yesterday. But he didn’t know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?

Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string “START”, this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian’s language. A line with a single string “END” indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string “START”, this string should be ignored, then an article written in Martian’s language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can’t find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(’ ‘), tab(’\t’), enter(’\n’) and all the punctuation should not be translated. A line with a single string “END” indicates the end of the book part, and that’s also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.

Output
In this problem, you have to output the translation of the history book.

Sample Input
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i’m fiwo riwosf.
i fiiwj fnnvk!
END

Sample Output
hello, i’m from mars.
i like earth!

Hint

Huge input, scanf is recommended.
题意
根据字典翻译文章
分析

代码

#include<iostream>
#include <stdio.h>
#include<algorithm> 
#include<cstring>
using namespace std;
const int N=100005;
int son[N][27],idx;
char cnt[N][20];
char str2[5005];
void insert(char str[],char s[])
{
	int p=0;
	for(int i=0;str[i];++i){
		int u=str[i]-'a';
		if(!son[p][u])son[p][u]=++idx;
		p=son[p][u];
	}
	strcpy(cnt[p],s);
}
void query(char str[])
{
	for(int i=0;str[i];i){
		int ii=i;
		while(str[ii]<='z'&&str[ii]>='a'&&str[ii])ii++;
		int p=0;int flag=0;
		for(int j=i;j<ii;++j){
			int u=str[j]-'a';
			if(!son[p][u]){
				flag=1;break;
			}
			p=son[p][u];
		}
		if(cnt[p]=="")flag=1;
		if(flag){
			for(int j=i;j<ii;++j)cout<<str[j];
		}
		else{
			cout<<cnt[p];
		}
		while(str[ii]&&(str[ii]<'a'||str[ii]>'z'))cout<<str[ii++];
		i=ii;
	}
}
int main(){
	char str[20];cin>>str;
	memset(str2,0,sizeof(str2));
	memset(cnt,0,sizeof(cnt));
	while(scanf("%s",str)&&strcmp(str,"END"))
	{
		char s[20];scanf("%s",s);
		insert(s,str);
	}
	cin>>str;
	while(gets(str2)&&strcmp(str2,"END"))
	{
		query(str2);
		cout<<endl;
	}
	return 0;
} 

-----------------------------------------------------------------------------------------

posted @ 2022-11-17 23:04  林动  阅读(24)  评论(0编辑  收藏  举报