XYNUOJ 第四次考试(STL模块)

问题 A: 胜利大逃亡

时间限制: 2 Sec   内存限制: 33 MB
提交: 1   解决: 1
[ 提交][ 状态][ 讨论版]

题目描述

Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.

魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.


输入

输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙.(如果对输入描述不清楚,可以参考Sample Input中的迷宫描述,它表示的就是上图中的迷宫)

特别注意:本题的测试数据非常大,请使用scanf输入,我不能保证使用cin能不超时.在本OJ上请使用Visual C++提交.

输出

对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.


样例输入

1
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0

样例输出

11

提示

/*
	这道题是搜索题,广度优先搜索,在搜索的模块会讲 
*/
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int N = 55;

int map[N][N][N];
int vis[N][N][N];
int tx[] = {1,-1,0,0,0,0};
int ty[] = {0,0,1,-1,0,0};
int tz[] = {0,0,0,0,1,-1};
int a,b,c,t,ans;

struct Node
{
    int x,y,z,step;
};

int abs(int x)
{
    return x<0?-x:x;
}

int check(int i,int j,int k)//检查边界 
{
    if(i<0 || j<0 || k<0 || i>=a || j>=b || k>=c || map[i][j][k])
    return 0;
    return 1;
}

int bfs(int x,int y,int z)
{
    int i;
    queue<Node> Q;
    Node p,q;
    p.x = x;
    p.y = y;
    p.z = z;
    p.step = 0;
    vis[x][y][z] = 1;
    Q.push(p);
    while(!Q.empty())
    {
        p = Q.front();
        Q.pop();
        if(p.x == a-1 && p.y == b-1 && p.z==c-1 && p.step<=t)
        return p.step;
        for(i = 0;i<6;i++)
        {
            q = p;
            q.x+=tx[i];
            q.y+=ty[i];
            q.z+=tz[i];
            if(!vis[q.x][q.y][q.z] && check(q.x,q.y,q.z))
            {
                q.step++;
                vis[q.x][q.y][q.z] = 1;
                if(abs(q.x-a+1)+abs(q.y-b+1)+abs(q.z-c+1)+q.step>t)
                continue;
                Q.push(q);
            }
        }
    }
    return -1;
}

int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        int i,j,k;
        scanf("%d%d%d%d",&a,&b,&c,&t);
        memset(map,0,sizeof(map));
        memset(vis,0,sizeof(vis));
        for(i = 0;i<a;i++)
        for(j = 0;j<b;j++)
        for(k = 0;k<c;k++)
        scanf("%d",&map[i][j][k]);
        ans = bfs(0,0,0);
        printf("%d\n",ans);
    }

    return 0;
}

问题 B: 表达式求值

时间限制: 3 Sec   内存限制: 6 MB
提交: 13   解决: 8
[ 提交][ 状态][ 讨论版]

题目描述

实现输入一个表达式求出它的值的计算器,比如输入:“1+2/4=”,程序就输出1.50(结果保留两位小数)

输入

第一行输入一个整数n,共有n组测试数据(n<10)。 每组测试数据只有一行,是一个长度不超过1000的字符串,表示这个运算式,每个运算式都是以“=”结束。这个表达式里只包含+-*/与小括号这几种符号。其中小括号可以嵌套使用。数据保证输入的操作数中不会出现负数。 数据保证除数不会为0

输出

每组都输出该组运算式的运算结果,输出结果保留两位小数。

样例输入

2
1.000+2/4=
((1+2)*5+1)/4=

样例输出

1.50
4.00

提示

/*
	这道题是数据结构课本上的改进题
*/
#include <stack>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
using namespace std;

int priority(char c) {
	if(c == '=')    return 0;
	if(c == '+')    return 1;
	if(c == '-')    return 1;
	if(c == '*')    return 2;
	if(c == '/')    return 2;
	return 0;
}

void compute(stack<double>& Num,stack<char>& Op) {
	double b = Num.top();
	Num.pop();
	double a = Num.top();
	Num.pop();
	switch(Op.top()) {
		case '+':
			Num.push(a+b);
			break;
		case '-':
			Num.push(a-b);
			break;
		case '*':
			Num.push(a*b);
			break;
		case '/':
			Num.push(a/b);
			break;
	}
	Op.pop();
}

int main() {
	int z;
	char str[1005];
	stack<double> Num;
	stack<char> Op;
	scanf("%d",&z);
	while(z--) {
		scanf("%s",str);
		int len = strlen(str);
		for(int i=0; i<len; i++) {
			if(isdigit(str[i])) {
				double n = atof(&str[i]);
				while(i<len && (isdigit(str[i]) || str[i]=='.'))
					i++;
				i--;
				Num.push(n);
			} else {
				if(str[i] == '(')
					Op.push(str[i]);
				else if(str[i] == ')') {
					while(Op.top()!='(')
						compute(Num,Op);
					Op.pop();
				} else if(Op.empty() || priority(str[i])>priority(Op.top()))
					Op.push(str[i]);
				else {
					while(!Op.empty() && priority(str[i])<=priority(Op.top()))
						compute(Num,Op);
					Op.push(str[i]);
				}
			}
		}
		Op.pop();
		printf("%.2f\n",Num.top());
		Num.pop();
	}
	return 0;
}

问题 C: Train

时间限制: 1 Sec   内存限制: 33 MB
提交: 12   解决: 2
[ 提交][ 状态][ 讨论版]

题目描述

As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.

输入

The input contains several test cases. Each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:O1, and the order of the trains leave:O2. The input is terminated by the end of file. More details in the Sample Input.

输出

The output contains a string "No." if you can't exchange O2 to O1, or you should output a line contains "Yes.", and then output your way in exchanging the order(you should output "in" for a train getting into the railway, and "out" for a train getting out of the railway). Print a line contains "FINISH" after each test case. More details in the Sample Output.

样例输入

3 123 321
3 123 312

样例输出

Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

提示

/*
	用栈模拟火车进站的问题 
*/
#include<iostream>
#include<stack>
#define max 100
using namespace std;
int main() {
	stack<char>s;
	int n,i,j,k,result[max];
	char str1[max],str2[max];
	while(cin>>n>>str1>>str2) {
		j=0,i=0,k=1;
		s.push(str1[0]);
		result[0]=1;
		while(i<n&&j<n) { //没有判断到字符串尾 
			if(s.size()&&s.top()==str2[j]) {
				j++;
				s.pop();
				result[k++]=0;
			} else {
				if(i==n) 
					break;
				s.push(str1[++i]);
				result[k++]=1;
			}
		}
		if(i==n) {
			cout<<"No."<<endl;
		} else {
			cout<<"Yes."<<endl;
			for(i=0; i<k; i++)
				if(result[i])
					cout<<"in"<<endl;
				else
					cout<<"out"<<endl;
		}
		cout<<"FINISH"<<endl;
	}
	return 0;
}

问题 D: 情报分析

时间限制: 1 Sec   内存限制: 12 MB
提交: 9   解决: 6
[ 提交][ 状态][ 讨论版]

题目描述

“八一三”淞沪抗战爆发后,*几次准备去上海前线视察和指挥作战。但都因为宁沪之间的铁路和公路遭到了敌军的严密封锁,狂轰滥炸,一直未能成行。 特科组织,其主要任务是保卫的安全,了解和掌握敌方的动向。经过一段时间的监听,谍报组获取了敌方若干份密报,经过分析,发现密文中频繁出现一些单词,情报人员试图从单词出现的次数中,推出敌军的行动计划。 请你编程,快速统计出频率高的前十个单词。

输入

密文是由英语单词(小写字母)组成,有若干段。单词之间由一个或多个空格分开,自然段之后可以用一个“,”或“。”表示结束。整个内容的单词数量不超过10000,不同的单词个数不超过500.

输出

输出占10行,每行一个单词及出现的次数,中间一个空格。要求按频率降序输出,出现次数相同的单词,按字典序输出。

样例输入

shooting is at shanghai station. shooting must 
be carried out. shooting shooting. 
shanghai station must be surrounded, at least a team of one hundred soldiers to fight. twenty five soldiers shooting in the north, twenty five soldiers shooting in the south, twenty five soldiers shooting in the east, twenty five soldiers shooting in the west. 

样例输出

shooting 8 
soldiers 5 
five 4 
in 4 
the 4 
twenty 4 
at 2 
be 2 
must 2 
shanghai 2 

提示

#include<iostream>
#include<bits/stdc++.h>//万能头文件,包含许多头文件,但是会影响程序 
using namespace std;
typedef pair<string, int> PAIR;
struct CmpByValue{
	bool operator()(const PAIR& leftValue, const PAIR& rightValue){
		return leftValue.second > rightValue.second? 1:(leftValue.second != rightValue.second)?0:leftValue.first<=rightValue.first;
	}
};
map<string ,int> mapWord;
int main(){
	string str;
	while(cin>>str){
		if(str[str.length()-1] == ',' || str[str.length()-1] == '.')
			str = str.substr(0,str.length()-1);
		mapWord[str]++;
	}
	vector<PAIR> vec(mapWord.begin(), mapWord.end());
	sort(vec.begin(), vec.end() ,CmpByValue());

	int i = 0;
	for(auto it=vec.begin(); it!=vec.end(); ++it,++i){
		if(i > 9)break;
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}

问题 E: Information Sharing

时间限制: 3 Sec   内存限制: 65 MB
提交: 6   解决: 5
[ 提交][ 状态][ 讨论版]

题目描述

There is going to be a test in the kindergarten. Since the kids would cry if they get a low score in the test, the teacher has already told every kid some information about the test in advance. But the kids are not satisfied with the information teacher gave. They want to get more. On the testing day, some kids arrived to the classroom early enough, and then shared his/her information with another. kids are honest, if A shares with B, B can get all the information A knows, so does A. At first the classroom is empty. As time pass by, a kid would arrive, or share information with other. However, the teacher hides somewhere, watching everything. She wants to know how much information some kid has gotten.

输入

There are multiple cases. The first line of each case contains an integer n, indicating there is n actions. The following n actions contain 3 types. 1: "arrive Name m a1 a2 ..am", means the kid called Name arrives at the classroom. He has m information, their id is a1 a2 ...am. 2: "share Name1 Name2", means that the kids called Name1 and Name2 share their information. (The sharing state will keep on, that means, if A share with B, later B share with C, A can also get all C's information via B. One kid may share with himself, but it doesn't mean anything.) 3: "check Name", means teacher wants to know the number of information kid called Name has got. n is less than 100000, and is positive. The information id is among [0,1000000]. Each Name has at most 15 characters. There would appears at most 1000 distinct information. Each kid carry no more than 10 information when arriving(10 is included).

输出

For every "check" statement, output a single number. If there's no check statement, don't output anything.

样例输入

8
arrive FatSheep 3 4 7 5
arrive riversouther 2 4 1
share FatSheep riversouther
check FatSheep
arrive delta 2 10 4
check delta
share delta FatSheep
check riversouther

样例输出

4
2
5

提示

check 1: FatSheep has 1 4 5 7, having all the information. So answer is 4.
check 2: delta has only 4 10 , doesn't have 1 5 7. So answer is 2
check 3: riversouther has 1 4 5 7 10, having all the information. So answer is 5

/*
	并查集与set
*/
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
int cnt;
map<string,int> mp;
const int maxn=100005;
int father[maxn];
set<int> s[maxn];
void init(int i) {
	father[i]=i;
}
int find(int p) {
	return p==father[p]?p:father[p]=find(father[p]);
}
void unio(int a,int b) {
	int fa=find(a),fb=find(b);
	if(fa!=fb) {
		father[fa]=fb;
		for(set<int>::iterator it=s[fa].begin(); it!=s[fa].end(); ++it) {
			s[fb].insert(*it);
		}
		s[fa].clear();
	}
}
int main() {
	int n;
	while(scanf("%d",&n)!=EOF) {
		char a[20],b[20];
		mp.clear();
		cnt=0;
		while(n--) {
			scanf("%s",a);
			if(a[0]=='a') {
				scanf("%s",b);
				mp[string(b)]=++cnt;
				init(cnt);
				s[cnt].clear();
				int m;
				scanf("%d",&m);
				for(int i=0; i<m; ++i) {
					int x;
					scanf("%d",&x);
					s[cnt].insert(x);
				}
			} else if(a[0]=='s') {
				char c[20];
				scanf("%s%s",b,c);
				int x=mp[string(b)],y=mp[string(c)];
				unio(x,y);
			} else {
				scanf("%s",b);
				int f=find(mp[string(b)]);
				printf("%d\n",s[f].size());
			}
		}
	}
	return 0;
}

问题 G: 懒省事的小名

时间限制: 1 Sec   内存限制: 12 MB
提交: 122   解决: 45
[ 提交][ 状态][ 讨论版]

题目描述

小名总结了一个英语近义词的词典(很厚哦,有多厚,你猜),词典实在太厚了,你需要帮助他写一个程序,输入一个单词,输出它的近义词

输入

第一行输入近义词的个数N和小名要查询的行数M

接下来N行为近义词对

M行单词,根据单词,输出它的近义词

输出

输出近义词,每个近义词占一行

样例输入

2 2
abc	cba
aaa	bbb
abc
aaa

样例输出

cba
bbb

提示

#include<iostream>
#include<vector>
#include<map>
using namespace std;
int main() {
	char s1[2000],s2[2000];
	string str1,str2;
	map<string, string> map_key;
	map<string, string> map_value;
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=0; i<n; ++i) {
		scanf("%s %s",s1,s2);
		str1 = s1;
		str2 = s2;
		map_key[str1] = str2;
		map_value[str2] = str1;
	}
	vector<string> vec;
	for(int i=0; i<m; ++i) {
		scanf("%s",s1);
		str1 = s1;
		vec.push_back(str1);
	}
	for(auto it=vec.begin(); it!=vec.end(); it++) {
		int mark = 0;
		if(map_key.find((*it)) != map_key.end()) {
			cout<<map_key[(*it)]<<endl;
			continue;
		} else {
			mark = 1;
		}
		if(map_value.find((*it)) != map_value.end()) {
			cout<<map_value[(*it)]<<endl;
			continue;
		} else {
			mark = 1;
		}
		if(mark)cout<<"Null\n";//测试数据中没有匹配失败的情况,太水 
	}
	return 0;
}
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
struct zidian{
    string a1;
    string a2;
}b[1000];
int main()
{
    int N,M;
    string str;
    scanf("%d %d",&N,&M);
    for(int i=0;i<N;i++)
        cin>>b[i].a1>>b[i].a2;
    for(int j=0;j<M;j++)
    {
        cin>>str;
        for(int k=0;k<N;k++)
        {
            if(str==b[k].a1)
            cout<<b[k].a2<<endl;
            if(str==b[k].a2)
            cout<<b[k].a1<<endl;
        }
    } 
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值