题目:
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以及
代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
vector<string> a;
int tot = 0;
char c;
map <long long, int> m;
const int inf = 1e9;
long long f(string x) {
long tmp = 0;
for (int i = 0; i < x.length(); i++) {
tmp=(tmp+(long long)(x[i])*7)%inf;
tmp=tmp*7%inf;
}
return tmp;
}
string s;
int main() {
string t1,t2;
while (1) {
getline(cin, s);
if(s == "@END@") break;
t1 = s.substr(0, s.find("] ")+1);
t2 = s.substr(s.find("] ")+2, s.length() - s.find("] ") - 1);
a.push_back(t1);
a.push_back(t2);
m.insert({f(t1), tot++});
m.insert({f(t2), tot++});
}
int N;
cin >> N;
getline(cin, s);
while (N--) {
getline(cin, s);
if(m.find(f(s))!=m.end()){
if(s[0]=='['){
string t=a[m[f(s)]+1];
cout<<t<<endl;
}else{
string t=a[m[f(s)]-1];
cout<<t.substr(1,t.length()-2)<<endl;
}
}else{
cout<<"what?"<<endl;
}
}
return 0;
}
B
题目:
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
思路:
利用字典树进行求解,每输入一个字符串,便插入到一颗字典树中,同时判断该字符串是否是已插入字符串的子串或者存在其他已插入字符串是该字符串的子串。若上述条件成立,则该组其他字符串可不用再一一插入字典树。
代码:
#include<bits/stdc++.h>
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));
root=tot=0;
}
void clear()
{
memset(child,-1,sizeof(child));
root=tot=0;
}
int insert(string str){
int now=root,jud=0,len=str.size();
for(int i=0;i<len;i++){
int x=str[i]-'0';
if(child[now][x]==-1){
child[now][x]=++tot;
flag[now]=0;
}
else if(i==len-1||flag[child[now][x]]) jud=1;
now=child[now][x];
}
flag[now]=1;
return jud;
}
};
int main()
{
Trie t;
string s;
int f=0,g=0;
while(cin>>s)
{
//cout <<s <<endl;
f=0;
g++;
t.clear();
if(t.insert(s)==1)
f=1;
while(1)
{
cin >>s;
if(s=="9")
break;
if(t.insert(s))
f=1;
}
if(f==1)
printf("Set %d is not immediately decodable\n",g);
else
printf("Set %d is immediately decodable\n",g);
}
}
C
题目:
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算法找出前缀和,
记Next【x-1】为now
情况1:P【x】=P【now】,Next[x]=now+1
情况2:P【x】≠P【now】,尝试缩短now,并且保证对P[0…x-1]而言,它的now-真前缀等于now-真后缀。
代码:
#include<iostream>
#include<string.h>
using namespace std;
int n,Next[10005];
char ptr[10005],str[1000005];
void getNext(const char ptr[],int len)
{
Next[0]=0;
for(int i=1,j=0;i<len;i++)
{
while(j&&ptr[i]!=ptr[j]) j=Next[j-1];
if(ptr[i]==ptr[j]) j++;
Next[i]=j;
}
}
int KMP(const char str[],const char ptr[])
{
int len1=strlen(str);
int len2=strlen(ptr);
int cnt=0;
getNext(ptr,len2);
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];
}
}
return cnt;
}
void clear()
{
memset(str,0,sizeof str);
memset(ptr,0,sizeof(ptr));
memset(Next,0,sizeof(Next));
}
int main()
{
cin >> n;
while(n--)
{
clear();
cin >> ptr >> str;
cout << KMP(str,ptr) <<endl;
}
}