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

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


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

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


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

#include <cstdio>
#include <cstring>
#include <algorithm>
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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值