PAT甲级1039,1040解题报告

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 N​i​​ (≤200) are given in a line. Then in the next line, 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

题目大意:告诉选课信息,输出每个学生的选课信息。

解题思路:水题,一开始没注意随便用数组遍历,最后一个样例超时,改成哈希表就过了。没什么好说的,看代码

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
string name[40005];
map<string, vector<int>> r;
int main()
{
	int n, k;
	cin >> n >> k;
	for (int i = 0; i < k; i++) {
		int index;
		int tmp;
		cin >> index >> tmp;
		for (int j = 0; j < tmp; j++) {
			string cur;
			cin >> cur;
			r[cur].push_back(index);
		}
	}
	for (int i = 0; i < n; i++) {
		cin >> name[i];
	}
	for (int i = 0; i < n; i++) {
		cout << name[i] << " ";
		if (r[name[i]].size() != 0)
			cout << r[name[i]].size() << " ";
		else
			cout << 0 << endl;
		sort(r[name[i]].begin(), r[name[i]].end());
		for (int j = 0; j < r[name[i]].size(); j++) {
			if (j != r[name[i]].size() - 1)
				cout << r[name[i]][j] << " ";
			else
				cout << r[name[i]][j] << 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

题目大意:给一个字符串,问你其中含有的最长的子串是回文串,然后回文串的长度是多少。

解题思路:。。。这题比较气,一开始就陷入dp的思想了,开始一顿分析。递推式,初始条件一写,想都没想把它当成一个很简单的dp问题:区间模型。因为只有两种操作,头上去一个,尾部去一个。别的没什么操作了。。。后来写完交上去,错了三个样例,。。后来发现想错了,其实暴力就可以了,不需要dp的。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
//int dp[1005][1005];
//int getMin(string s) {
	//int len = s.size();
	//for (int i = 0; i < len; i++) {
		//dp[i][i] = 0;
		//if (s[i] == s[i + 1] && i + 1 < len) {
			//dp[i][i + 1] = 0;
		//}
		//else if (s[i] != s[i + 1] && i + 1 < len) {
			//dp[i][i + 1] = 1;
		//}
	//}
	//for (int i = 2; i < len; i++) {
		//for (int j = 0; j < len-i; j++) {
		//	if (s[j] == s[j + i])dp[j][j + i] = dp[j + 1][j + i - 1];
			//else if (s[j] != s[j + i]) {
				//dp[j][j + i] = min(dp[j + 1][j + i], dp[j][j + i - 1]) + 1;
			//}
		//}
	//}
	//return dp[0][len - 1];
//}
int main()
{
	string cur;
	getline(cin, cur);
	//cur[i]=cur[j] dp[i][j+1]=dp[i+1][j]=0;
	//cur[i]!=cur[j]
	//dp[i][j+1]=min{dp[i][j],dp[i+1][j+1]}+1
	int max = -1;
	for (int i = 0; i < cur.size(); i++) {
		for (int j = cur.size() - 1; j >= i&&j - i + 1 > max; j--) {
			string tmp = cur.substr(i, j - i + 1);
			string rev = tmp;
			reverse(rev.begin(), rev.end());
			if (tmp == rev)max = j - i + 1;
		}
	}
	cout<<max<<endl;
	return 0;
}

其实仔细想想,dp应该也是可以的,只是我们要限制的更死一点,要求严格连续子序列对称,这里就不写了。

猜你喜欢

转载自blog.csdn.net/TateBrwonJava/article/details/83046990