【Week 15 作业】A - ZJM 与霍格沃兹(必做)、B - ZJM 与生日礼物(选做)、C - ZJM 与纸条(选做)

A - ZJM 与霍格沃兹(必做)

题意:

ZJM 为了准备霍格沃兹的期末考试,决心背魔咒词典,一举拿下咒语翻译题
题库格式:[魔咒] 对应功能
背完题库后,ZJM 开始刷题,现共有 N 道题,每道题给出一个字符串,可能是 [魔咒],也可能是对应功能
ZJM 需要识别这个题目给出的是 [魔咒] 还是对应功能,并写出转换的结果,如果在魔咒词典里找不到,输出 “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?

思路做法:

正常思维是直接用2个map将原文和译文相互记录下来,但是字符串可能很长,因此全部记录下来会爆空间,将字符串用哈希算法转化为整形数,可以降低空间消耗。

总结:

最初实现后仍然是爆空间,原因可能是我在存储映射时,把原文的左右括号也存进去了,去掉后就过了。

代码:

#include <iostream>
#include <string>
#include <map>
using namespace std;
/*
map<string, string> 会超空间 
把string通过哈希转成数字  空间大大减小 
*/
int seed = 7, mod = 1e9+7;
map<int, string> str1, str2;
int str2int(string str){
	int len = str.size(), ans = 0, sd = seed;
	for(int i = 0; i < len; ++i){
		ans += int(str[len-i-1])*sd % mod;
		sd = sd * seed % mod;
	}
	return ans;
}
int main(){
	string sent, curse, feature;
	while(getline(cin, sent)){
		if(sent == "@END@") break;
		int sz = sent.size(), i = 1;
		for(; i < sz; ++i){
			if(sent[i] == ']') break;
		}
		curse = sent.substr(1, i-1);
		int hash1 = str2int(curse);
//		cout << curse << endl;
		feature = sent.substr(i+2, sz - i-2);
		int hash2 = str2int(feature);
//		cout << feature << endl;
		str1[hash1] = feature;
		str2[hash2] = curse;
	}
	int n; scanf("%d ", &n);
	for(int i = 0; i < n; ++i){
		string test;
		int hash;
		getline(cin, test);
		if(test[0] == '['){
			hash = str2int(test.substr(1, test.size()-2));
		}else{
			hash = str2int(test);
		}
		string ans1 = str1[hash], ans2 = str2[hash];
		if(ans1 != "") cout << ans1 << endl;
		else if(ans2 != "") cout << ans2 << endl;
		else cout << "what?" << endl;
	}
	return 0;
} 

B - ZJM 与生日礼物(选做)

题意:

ZJM 收到了 Q老师 送来的生日礼物,但是被 Q老师 加密了。只有 ZJM 能够回答对 Q老师 的问题,Q老师 才会把密码告诉 ZJM。

Q老师 给了 ZJM 一些仅有 01 组成的二进制编码串, 他问 ZJM:是否存在一个串是另一个串的前缀.

Input

多组数据。每组数据中包含多个仅有01组成的字符串,以一个9作为该组数据结束的标志。

Output

对于第 k 组数据(从1开始标号),如果不存在一个字符串使另一个的前缀,输出"Set k is immediately decodable",否则输出"Set k is not immediately decodable"。
每组数据的输出单独一行

Sample Input

01
10
0010
0000
9
01
10
010
0000
9

Sample Output

Set 1 is immediately decodable
Set 2 is not immediately decodable

思路做法:

看一个字符串是否为另一个字符串的前缀,可以用字典树。因为题目仅要求判断是否存在而不要求计算出具体数目,因此在insert时判断一下,返回一个bool值即可。

总结:

对源代码的insert函数修改后也用不着query了。

代码:

#include <stdio.h>
#include <string.h>
/*
是否存在一个串是另一个串的前缀
字典树 
*/
const int N = 1e3+50, charset = 2;
struct Tree{
	int tot, root, child[N][charset], flag[N];
	// 节点编号,根,child[now][x]表示now的x子节点,结尾标志
	Tree(){  // 构造函数 
		memset(child, -1, sizeof(child));
		root = tot = 0;
	}
	void clear(){  // 清空 
		memset(child, -1, sizeof(child));
		root = tot = 0;
	}
	bool insert(char *s){  // 字典树中插入字符串 
		int now = root, cnt = 0, len = strlen(s);  // 从根开始
		for(int i = 0; i < len; ++i){
			int x = s[i] - '0';
			if(child[now][x] == -1){
				child[now][x] = ++tot;
				flag[now] = 0;
			}else if(i == len-1 || flag[child[now][x]]){
				// s是其他字符串的前缀  其他字符串是s的前缀 
				cnt = 1;
			}
			now = child[now][x];
		}
		flag[now] = 1;  // 标记结尾
		return cnt;
	}
	bool query(char *s){  // 查询是否有字符串是s的前缀 
		int now = root, len = strlen(s);  // 从根开始查询
		for(int i = 0; i < len; ++i){
			int x = s[i] - '0';
			if(child[now][x] == -1) return false;  // 不存在
			if(flag[x] == 1) return true;  // 前缀 
		}
		return false;
	}
};
int main(){
	char s[N]; int k = 1;
	Tree t; bool ans = 0, num = 0;
	while(~scanf("%s", s)){
		if(strcmp(s, "9") == 0){
			if(ans) printf("Set %d is not immediately decodable\n", k);
			else printf("Set %d is immediately decodable\n", k);
			t.clear(); k++; ans = 0; num = 0;
			continue;
		}
		num = t.insert(s);
//		printf("%d\n", num);
		if(num) ans = 1;
	} 
	return 0;
}

C - ZJM 与纸条(选做)

题意:

ZJM 的女朋友是一个书法家,喜欢写一些好看的英文书法。有一天 ZJM 拿到了她写的纸条,纸条上的字暗示了 ZJM 的女朋友 想给 ZJM 送生日礼物。ZJM 想知道自己收到的礼物是不是就是她送的,于是想看看自己收到的礼物在纸条中出现了多少次。

Input

第一行输入一个整数代表数据的组数

每组数据第一行一个字符串 P 代表 ZJM 想要的礼物, 包含英语字符 {‘A’, ‘B’, ‘C’, …, ‘Z’}, 并且字符串长度满足 1 ≤ |P| ≤ 10,000 (|P| 代表字符串 P 的长度).
接下来一行一个字符串 S 代表 ZJM 女朋友的纸条, 也包含英语字符 {‘A’, ‘B’, ‘C’, …, ‘Z’}, 满足 |P| ≤ |S| ≤ 1,000,000.

Output

输出一行一个整数代表 P 在 S中出现的次数.

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0

思路做法:

字符串匹配,暴力算超时,因此用KMP算法。具体算法思想及实现参见这里

总结:

KMP使算法复杂度由O(nm)降为O(n+m)

代码:

#include <stdio.h>
#include <string.h>
const int N = 1e4+50;
const int M = 1e6+50;
/*
计算 P在 S中出现的次数  S:主串,P:模式串 
KMP算法 O(n+m) 
next 数组定义为:next[i] 表示 P[0] ~ P[i] 这一个子串,
使得前k个字符恰等于后k个字符的最大的k
*/
int nxt[N];
void get_nxt(const char *p, int len){
	// 计算nxt数组值 
	nxt[0] = 0;
	for(int i = 1, j = 0; i < len; ++i){
		while(j && p[i] != p[j]) j = nxt[j-1];
		if(p[i] == p[j]) j++;
		nxt[i] = j;
	}
}
int kmp(const char *p, const char *s){
	int len1 = strlen(p), len2 = strlen(s);
	int j = 0, ans = 0;
	get_nxt(p, len1);
	for(int i = 0; i < len2; ++i){
		while(j && p[j] != s[i]) j = nxt[j-1];  // 失配
		if(p[j] == s[i]) j++;
		if(j == len1){
			ans++;
			j = nxt[j-1];  // 转移到下一个 
		}
	}
	return ans; 
} 
int main(){
	int n; scanf("%d", &n);
	char p[N], s[M];
	while(n--){
		scanf("%s", p); scanf("%s", s);
		printf("%d\n", kmp(p, s));
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

容嬷嬷当年一枝花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值