PAT 第三周*(1039-1041)

这几天玩的很嗨,和家里人好好耍了一趟,准备回校的片刻,又觉得超级无聊(剧更新的都追完了呜,就挑了几道不能构成一组的题目打发时间,马上下午就要踏上去南京回校的动车了。

目录

1039 Course List for Student (25 分)

1040 Longest Symmetric String (25 分)

1041 Be Unique (20 分)


1039 Course List for Student (25 分)

Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (≤40,000), the number of students who look for their course lists, and K (≤2,500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students {\color{DarkGreen} N_i} (≤200) are given in a line. Then in the next line, {\color{DarkGreen} N_i}​ student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student's name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.

Sample Input:

11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9

Sample Output:

ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0

这道题只要熟悉map数据结构直接A掉不是问题!

【本题代码】

#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
	int N, K;
	cin >> N >> K;
	string* query = new string[N];
	string* name;
	map<string,vector<int>> choose;
	for (int i = 0; i < K; i++)
	{
		int index,N_;
		cin >> index >> N_;
		name = new string[N_];
		for (int j = 0; j < N_; j++)
		{
			cin >> name[j];
			if (choose.find(name[j]) != choose.end())
				choose.find(name[j])->second.push_back(index);
			else {
				vector<int> c;
				c.push_back(index);
				choose.insert(pair<string, vector<int>>(name[j], c));
			}
		}
	}
	for (int i = 0; i < N; i++)
	{
		cin >> query[i];
		cout << query[i] << " ";
		if (choose.find(query[i]) != choose.end()) {
			vector<int> c = choose.find(query[i])->second;
			cout << c.size();
			sort(c.begin(), c.end());
			for (int j = 0; j < c.size(); j++)
				cout << " " << c[j];
			cout << endl;
		}
		else
			cout << 0 << endl;
	}
	return 0;
}

1040 Longest Symmetric String (25 分)

Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, given Is PAT&TAP symmetric?, the longest symmetric sub-string is s PAT&TAP s, hence you must output 11.

Input Specification:

Each input file contains one test case which gives a non-empty string of length no more than 1000.

Output Specification:

For each test case, simply print the maximum length in a line.

Sample Input:

Is PAT&TAP symmetric?

Sample Output:

11

本题实质:找最大回文子串。

首先,我的思考是上来想到从大到小递归查找(longestSymmetricSubstr函数),但意料之中t了,接着便考虑从回文的两种形式出发,也不知道大佬的标答怎样,先说一下我后来想到的A掉的思路。

回文子串长度无非奇数偶数,奇数的举个栗子:32123,偶数的便是321123,这次便换成从小到大查找,遍历字符串时对于每一个字符,把它作为回文子串的对称中心(偶数对应最中间两个数的右一个),这样遇到两边不等时便可结束比较,算法时间复杂度应该能够减小(结果符合预期)。其中对于偶数长度的回文子串要单独处理整个字符串就是回文的一个情况(测试点3),plus边界处理也要注意!

【本题代码】

#include<iostream>
#include<vector>
using namespace std;
int longestSymmetricSubstr(vector<char>, int, int);
int lSSubstr(vector<char>);
int main()
{
	char c;
	vector<char> str;
	c = getchar();
	while (c != '\n') {
		str.push_back(c);
		c = getchar();
	}
	//cout << longestSymmetricSubstr(str, 0, str.size() - 1);
	cout << lSSubstr(str);
	return 0;
}
int lSSubstr(vector<char> str)
{
	int L = 1;
	for (int i = 0; i < str.size(); i++) {
		int temp = min(i, int(str.size() - 1 - i));
		int j = 1;
		int k1 = 0, k2 = 0;
		int l1 = 0, l2 = 0;
		for (j = 1; j <= temp; j++) {
			if (!k1) {//有一个对称中心为str[i] 即奇数项
				if (str[i - j] != str[i + j])
					k1 = 1;
				else
					l1++;
			}
			if (!k2) {//无对称中心 即偶数项 以str[i-1],str[i]展开
				if (str[i - j] != str[i - 1 + j])
					k2 = 1;
				else
					l2++;
			}
			if (k1 && k2)
				break;
		}
		if (str.size() % 2 == 0 && i == str.size() / 2 && str[0] == str[str.size() - 1])//测试点3(无对称中心那一情况 疏漏的)
			l2++;
		L = max(L, 2 * l1 + 1);
		L = max(L, 2 * l2);
	}
	if (str[str.size()-1] == str[str.size() - 2])//无对称中心那一情况 疏漏的
		L = max(L, 2);
	return L;
}
int longestSymmetricSubstr(vector<char> str, int left, int right)//递归太超时了
{
	if (left > right || left < 0 || right > str.size() - 1)
		return 0;
	for (int i = 0; i <= (left + right) / 2 - left; i++)
		if (str[left + i] != str[right - i]) {
			return max(longestSymmetricSubstr(str, left + i, right - i - 1), longestSymmetricSubstr(str, left + i + 1, right - i));
		}
	return right - left + 1;
}

1041 Be Unique (20 分)

Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1,10^{4}]. The first one who bets on a unique number wins. For example, if there are 7 people betting on { 5 31 5 88 67 88 17 }, then the second one who bets on 31 wins.

Input Specification:

Each input file contains one test case. Each case contains a line which begins with a positive integer N (≤10^{5}) and then followed by N bets. The numbers are separated by a space.

Output Specification:

For each test case, print the winning number in a line. If there is no winner, print None instead.

Sample Input 1:

7 5 31 5 88 67 88 17

Sample Output 1:

31

Sample Input 2:

5 888 666 666 888 888

Sample Output 2:

None

本题的分值是这次博客里最低的(理应对应难度最低),确是我自己花时最长的,旨在想复杂了,一直在t,,,主要试了这几种:①map数据结构,发现map有序,不再是输入时的顺序,失败;②vector数据结构+algorithm库的count函数,测试点4 5超时,失败;③vector数据结构+visit数组标记+algorithm库的count函数,测试点4超时,失败④unordered_map数据结构,结果PAT编译器不支持,失败;⑤抱着试一试的心态,因为题目限定了所给数字≤10^{4},所以直接开一个int型数组来存放这些数字的出现次数,结果又想不到怎么找出输入时顺序的第一个unique,妥协到求助博客,才发现原来用数组存一下输入时的数用作索引即可,挺巧妙但也是常见的做法了,想不到还是因为做题少啊,这才成功

【本题代码】

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<unordered_map>
using namespace std;
int main()
{
	int n;
	cin >> n;
	int* t = new int[n];
	int m[10001] = { 0 };
	for (int i = 0; i < n; i++)
	{
		cin >> t[i];
		m[t[i]]++;
	}
	int i;
	for (i = 0; i < n; i++)
		if (m[t[i]] == 1)
		{
			cout << t[i];
			break;
		}
	if (i == n)cout << "None";
	return 0;
}
/*
	map<int,int> m;
	for (int i = 0; i < n; i++)
	{
		cin >> t;
		if (m.find(t) != m.end())
			m.find(t)->second = m.find(t)->second + 1;
		else
			m.insert(pair<int, int>(t, 1));
	}
	map<int, int>::iterator i;
	for (i = m.begin(); i != m.end(); i++)
		if (i->second == 1) {
			cout << i->first; break;
		}
	if (i == m.end())cout << "None";
*/
/*
for (i = 0; i < map.size(); i++)//后两个测试点超时
	if (count(map.begin(), map.end(), map[i]) == 1)
	{
		cout << map[i];
		break;
	}
*/
/*
	vector<int> map;
	bool* vis = new bool[n];
	for (int i = 0; i < n; i++)
	{
		cin >> t;
		map.push_back(t);
		vis[i] = false;
	}
	int i;

	for (i = 0; i < map.size(); i++) {//第四个测试点超时
		int key = 0;
		if (!vis[i])
			for (int j = 0; j < map.size(); j++)
				if (i != j && map[i] == map[j])
				{
					map[j] = true; key = 1; break;
				}
		if (!key) { cout << map[i]; break; }
	}
	if(i == map.size())
		cout << "None";
*/
/*
	unordered_map<int, int> m;//c++0x/c++11标准才把unordered_map纳入std标准
	for (int i = 0; i < n; i++)
	{
		cin >> t;
		if (m.find(t) != m.end())
			m.find(t)->second = m.find(t)->second + 1;
		else
			m.insert(pair<int, int>(t, 1));
	}
	unordered_map<int, int>::iterator i;
	for (i = m.begin(); i != m.end(); i++)
		if (i->second == 1) {
			cout << i->first; break;
		}
	if (i == m.end())
		cout << "None";
*/
/*
	vector<int> m;
	vector<int> vis;//运行超时
	for (int i = 0; i < n; i++)
	{
		cin >> t;
		if (count(m.begin(), m.end(), t) == 0) {
			m.push_back(t);
			vis.push_back(1);
		}
		else {
			vis[find(m.begin(), m.end(), t) -m.begin()]++;
		}
	}
	int i;
	for (i = 0; i < vis.size(); i++)
		if (vis[i] == 1)
		{
			cout << m[i];
			break;
		}
	if (i == vis.size())cout << "None";
*/

that’s all,开学快乐,虽然并不是很期待冬季学校,但是回学校收收心,准备保研事宜也是要事!新的一学期要更加努力呀,小禾祝大家一切顺意!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值