目录
B - ZJM 与生日礼物
题意
ZJM收到了Q老师送来的生日礼物,但是被 Q老师加密了。只有 ZJM 能够回答对Q老师的问题,Q老师才会把密码告诉 ZJM。
Q老师给了ZJM一些仅有 01组成的二进制编码串, 他问ZJM:是否存在一个串是另一个串的前缀.
思路
本题使用字典树。在基本字典树的基础上修改插入操作,使其在插入过程中检查是否访问到某一字符串的尾或插入的字符串是已存在字符串的前缀:只要有如此两种情况,则存在一个串是另一个串的前缀。
代码
#include<iostream>
#include<string.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(char* str){