PAT-A 1022. Digital Library (字符串模拟)

题目链接:https://www.patest.cn/contests/pat-a-practise/1022


题意:题意挺简单的就不说了。

用char*  100ms,用string就超时,果然string要慢好多,大量字符串时还是不要用。


有时间可以尝试用map写一发。

#include 
#include 
#include 
using namespace std;
int n, m;
struct node {
	int id;
	char title[100], author[100], key[100], pub[100], year[100];
}book[10010];
int ans[10010];
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%d", &book[i].id);
		getchar();
		gets(book[i].title);
		gets(book[i].author);
		gets(book[i].key);
		gets(book[i].pub);
		gets(book[i].year);
	}
	scanf("%d", &m);
	getchar();
	char qur[100];
	for(int i = 0; i < m; i++) {
		int cnt = 0;
		gets(qur);
		puts(qur);
		if(qur[0] == '1') {
			for(int j = 0; j < n; j++) {
				if(!strcmp(book[j].title, qur + 3)) {
					ans[cnt++] = book[j].id;
				}
			}
		}
		else if(qur[0] == '2') {
			for(int j = 0; j < n; j++) {
				if(!strcmp(book[j].author, qur + 3)) {
					ans[cnt++] = book[j].id;
				}
			}
		}
		else if(qur[0] == '3') {
			for(int j = 0; j < n; j++) {
				if(strstr(book[j].key, qur + 3)) {
					ans[cnt++] = book[j].id;
				}
			}
		}
		else if(qur[0] == '4') {
			for(int j = 0; j < n; j++) {
				if(!strcmp(book[j].pub, qur + 3)) {
					ans[cnt++] = book[j].id;
				}
			}
		}
		else if(qur[0] == '5') {
			for(int j = 0; j < n; j++) {
				if(!strcmp(book[j].year, qur + 3)) {
					ans[cnt++] = book[j].id;
				}
			}
		}
		if(!cnt) puts("Not Found");
		else {
			sort(ans, ans + cnt);
			for(int j = 0; j < cnt; j++) {
				printf("%07d\n", ans[j]);
			}
		}
	}
	return 0;
}


你可能感兴趣的:(pat,模拟)