魔咒词典 - 题解【暴搜】

题面

哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助。
给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”

Input

首先列出词典中不超过100000条不同的魔咒词条,每条格式为:

[魔咒] 对应功能

其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。
词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。

Output

每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”

Sample Input

[expelliarmus] the disarming charm
[rictusempra] send a jet of silver light to hit the enemy
[tarantallegra] control the movement of one's legs
[serpensortia] shoot a snake out of the end of one's wand
[lumos] light the wand
[obliviate] the memory charm
[expecto patronum] send a Patronus to the dementors
[accio] the summoning charm
@END@
4
[lumos]
the summoning charm
[arha]
take me to the sky

Sample Output

light the wand
accio
what?
what?

Time limit

5000 ms

Memory limit

32768 kB

题解

我想这道题的本意可能是想检验对字符串哈希、map容器的使用的,但是我在一发TLE以及无限发MLE以后,用一个我自己也没想到会过的方法过了这道题……遂在此分享。

首先我们假设评测机1ms执行1000次O(1)级别的指令(这里考虑到了常数)。我用循环开1000次方进行了测试,考虑到开方并不是一个O(1)的指令,因此如此考虑也算是比较合理的。

题面读入至多1e5个条目,查询1e3次,考虑最坏情况,每次查询都遍历一遍条目,共需1e8次O(1)指令,根据这套方案换算下来就是1e5ms的时间……嗯,是限制时间的两倍。不过这是最坏情况,如果数据比较友好放我过去了呢?事实证明我猜对了,这道题的查询部分完全可以一个暴力解决。

起先我用map和string,成功收获了好多枚MLE,还在一个不知道咋回事的地方收获了一发TLE,后来听已经过题的人们说:

(1)不能用string类,会炸空间。

(2)我开了个string二维数组,打暴力,过了。

于是我结合他们的说法,改成了char数组,用C风格的方式,暴力求解。

现在,坐稳了,要开始操作了!

首先,我们开一个二维指针数组,然后开几个看起来比较奇怪的变量。

char *dic[100001][2];
int tot;
char l[110];	//line
char *s, *f; 	//spell,function

然后读入。这里按照整行读入,由于题目在输出的时候法术名称不需要带方括号“[]”,就先在这里做了个预处理。

gets(l);//我知道这是危险操作,但是问题不大
while (strcmp(l, "@END@")) {
	int len = strlen(l);
	rep(i, 0, 21) {
		if (l[i] == ']') {
			l[i] = '\0';
          	//这儿还有操作
			break;
		}
	}
	//一些其他的操作
	gets(l);
}

 接下来……请欣赏。

char *a, *b;
int sz;
a = l + 1;
rep(i, 0, 21) {
	if (l[i] == ']') {
		b = l + i;
		l[i] = '\0';
		sz = i;
		break;
	}
}
s = (char*)malloc((sz + 2) * sizeof(char));
f = (char*)malloc((len - sz + 2) * sizeof(char));
strcpy(s, a);
strcpy(f, b + 2);
dic[++tot][0] = s;
dic[tot][1] = f;

 我们知道C风格字符串实际上的操作入口是指针,字符串从指针指向的位置开始,到第一个空字符结束。所以我们读入一行存入l之后,手动打上空字符,然后指针指向不同区域。接着,动态分配空间!字符串拷贝!把内存空间和二维指针数组联系起来!虽然看起来略显智障(而且后来我想到更C++的方法其实是new 和 delete),但是确实有奇效!超省空间!

然后就是一个打暴力了。这里我用到了第二维不是0就是1的特性,使用了取非操作。完整代码如下:

#include <bits/stdc++.h>
#define grp int T;cin>>T;while(T--)
#define elif else if
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define rrep(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;

char *dic[100001][2];
int tot;

int main() {

	char l[110];
	gets(l);
	char *s, *f;
	while (strcmp(l, "@END@")) {
		int len = strlen(l);
		char *a, *b;
		int sz;
		a = l + 1;
		rep(i, 0, 21) {
			if (l[i] == ']') {
				b = l + i;
				l[i] = '\0';
				sz = i;
				break;
			}
		}
		s = (char*)malloc((sz + 2) * sizeof(char));
		f = (char*)malloc((len - sz + 2) * sizeof(char));
		strcpy(s, a);
		strcpy(f, b + 2);
		dic[++tot][0] = s;
		dic[tot][1] = f;
		gets(l);
	}
	int n;
	scanf("%d", &n);
	getchar();
	rep(i, 1, n) {
		gets(l);
		char *ptr = l;
		int len = strlen(l);
		int d;
		if (l[0] == '[') {
			d = 0;
			ptr = l + 1;
			l[len - 1] = '\0';
		} else {
			d = 1;
		}
		bool fnd = false;
		rep(i, 1, tot) {
			if (!strcmp(ptr, dic[i][d])) {
				puts(dic[i][!d]);
				fnd = true;
				break;
			}
		}
		if (!fnd) {
			puts("what?");
		}
	}

	return 0;
}

 

哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈咳咳咳咳咳……

话说到最后:该学的东西还是要学的!这只是一些歪门邪道,大家看个乐呵就好。

 

 

posted @ 2021-08-12 19:13  AlexHoring  阅读(72)  评论(0编辑  收藏  举报