bestcoder百度之星2024AK 1001&1002&1003&1004 题解(1)

/*

void Del(char* str)

{

bool f=Search(str);

if (f == false) return;

else

{

Trie *p=root;

int len=strlen(str);

for (int i=0;i<len;i++)

{

int id=str[i]-‘a’;

p=p->next[id];

p->vis=false;

}

}

}*/

void Del(char* str)

{

int cnt=Search(str);

if (cnt == 0) return;

else

{

Trie *p=root;

int len=strlen(str);

for (int i=0;i<len;i++)

{

int id=str[i]-‘a’;

p=p->next[id];

p->vis-=cnt;

}

for (int i=0;i<26;i++) p->next[i]=NULL;

}

}

int main()

{

int t;

scanf(“%d”,&t);

// getchar();

root=CreateNode();

while (t–)

{

char str1[7],str2[45];

scanf(“%s%s”,str1,str2);

switch(str1[0])

{

case ‘i’:

Insert(str2);

break;

case ‘s’:

printf(“%s\n”,Search(str2)>0?“Yes”:“No”);

break;

case ‘d’:

Del(str2);

}

}

return 0;

}

1004、

分析:先标准化,都让字符串从小到大排序,这样考虑的情况就减少了很多,开始我的想法用map容器处理,TLE。。。无语,不知道为什么最近用map总是超时闹bug。。。郁闷;之后换个方向思考,字典树(具体可以见我前面的博客),裸的字典树,只需处理插入查找便可

#include

#include

#include

#include

#include

#include

using namespace std;

struct TrieNode

{

bool vis;

struct TrieNode *next[26];

int id;

};

typedef TrieNode Trie;

Trie *root;

Trie *CreateNode()

{

Trie p=(Trie)malloc(sizeof(Trie));

p->vis=false;

p->id=0;

for (int i=0;i<26;i++)

p->next[i]=NULL;

return p;

}

void Insert(string str)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值