Week15 - 程序设计思维与实践 - 字符串算法

题目链接

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?

思路

利用哈希算法,将输入的字符串转换为数字,以实现快速比较。
建立两个 map 来存储对应信息,然后根据查询的输入来判断是 [魔咒] 还是功能,
最后再在对应的 map 中直接查询即可。

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
const int maxn=1e5+10;
typedef long long ll;
const ll mod=1e9+7;
const ll seed=7;
string t;
map<ll,string> mp1,mp2;

ll Hash(string str){
	ll hash=0;
	int sz=str.size();
	for(int i=0;i<str.size();i++){
		ll tmp=pow(seed,sz-i)*(ll)(str[i]);
		hash+=tmp%mod;
	} 
	return hash%mod;
}

int main()
{
//	freopen("out.txt","w",stdout);
	while(1){
		getline(cin,t);
		if(t=="@END@")
			break;
		string a,b;
		bool flag=false;
		for(int i=1;i<t.size();i++){
			if(t[i]==']'){
				flag=true;
				i++;
				continue;
			}
			if(!flag) a+=t[i];
			else b+=t[i];
		}
		mp1[Hash(a)]=b;
		mp2[Hash(b)]=a;
	}
	int n;
	cin>>n;
	getchar();
	for(int i=1;i<=n;i++){
		getline(cin,t);
		map<ll,string>::iterator it;
		if(t[0]=='['){
			string tmp;
			for(int i=1;i<t.size()-1;i++)
				tmp+=t[i];
			it=mp1.find(Hash(tmp));
			if(it!=mp1.end())
				cout<<it->second<<endl;
			else
				cout<<"what?"<<endl;
		}
		else{
			it=mp2.find(Hash(t));
			if(it!=mp2.end())
				cout<<it->second<<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

思路

字典树的简单应用,只需在插入的过程中进行判断即可:

  • 往字典树中依次插入每个字符串,假设当前字符串为 S
  • S 是否为之前某个字符串的前缀?
    • 当 S 插入结束后,其最后一个节点是字典树中已经存在的节点
  • 之前是否有某个字符串是 S 的前缀?
    • 在 S 的插入过程中,遇到某一个节点为某一个字符串的结尾。

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <algorithm>
using namespace std;

struct Trie{
	static const int N=1010,charset=2;
	int tot,root,child[N][charset],flag[N];
	Trie(){
		memset(child,-1,sizeof(child));
		memset(flag,0,sizeof(flag));
		root=tot=0;
	}
	void clear(){
		memset(child,-1,sizeof(child));
		memset(flag,0,sizeof(flag));
		root=tot=0;
	}
	bool insert(string str){
		int now=root,len=str.size();
		bool judge=false;
		for(int i=0;i<len;i++){
			int x=str[i];
			if(child[now][x]==-1){
				child[now][x]=++tot;
				flag[now]=0;
			}
			else if(i==len-1||flag[child[now][x]])	judge=true;
			now=child[now][x];
		}
		flag[now]=1;
		return judge;
	}
};

int main()
{
	//freopen("out.txt","w",stdout);
	int k=0;
	Trie a;
	string t;
	bool flag=false;
	while(cin>>t){
		if(t=="9"){	
			a.clear();
			if(flag)
				printf("Set %d is not immediately decodable\n",++k);
			else
				printf("Set %d is immediately decodable\n",++k);
			flag=false;
			continue;
		}
		if(a.insert(t))
			flag=true;
	}
	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

思路

P 在 S 中出现的次数,裸的 KMP 算法。

代码实现

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
const int maxn=1e6+10;
char str[maxn],ptr[maxn];
int n,Next[maxn];

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%s%s",ptr,str);
		int len1=strlen(str),len2=strlen(ptr),cnt=0;
		Next[0]=0;
		for(int i=1,j=0;i<len2;i++){
			while(j&&ptr[i]!=ptr[j]) j=Next[j-1];
			if(ptr[i]==ptr[j]) j++;
			Next[i]=j;
		}
		for(int i=0,j=0;i<len1;i++){
			while(j&&str[i]!=ptr[j]) j=Next[j-1];
			if(str[i]==ptr[j]) j++;
			if(j==len2){
				cnt++;
				j=Next[j-1];
			}
		}
		printf("%d\n",cnt);		
	}
	return 0;	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值