日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Problem C(HDU-5687)

發布時間:2025/3/17 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Problem C(HDU-5687) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Problem Description

度熊手上有一本神奇的字典,你可以在它里面做如下三個操作:?

? 1、insert : 往神奇字典中插入一個單詞?

? 2、delete: 在神奇字典中刪除所有前綴等于給定字符串的單詞?

? 3、search: 查詢是否在神奇字典中有一個字符串的前綴等于給定的字符串?

Input

這里僅有一組測試數據。第一行輸入一個正整數N(1≤N≤100000),代表度熊對于字典的操作次數,接下來N行,每行包含兩個字符串,中間中用空格隔開。第一個字符串代表了相關的操作(包括: insert, delete 或者 search)。第二個字符串代表了相關操作后指定的那個字符串,第二個字符串的長度不會超過30。第二個字符串僅由小寫字母組成。

Output

對于每一個search 操作,如果在度熊的字典中存在給定的字符串為前綴的單詞,則輸出Yes 否則輸出 No。

Sample Input

5
insert hello
insert hehe
search h
delete he
search hello

Sample Output

Yes
No

思路:帶有刪除操作的字典樹

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define N 500001 #define LL long long const int MOD=20091226; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std; int tot = 1; struct Node{int sum;//前綴int next[26];//子節點void init(){sum=0;memset(next,-1,sizeof next);} }tire[N]; void insert(char *str){int len=strlen(str);int root=0;for(int i=0;i<len;i++){int x=str[i]-'a';if(tire[root].next[x]==-1)tire[root].next[x]=tot++;root=tire[root].next[x];tire[root].sum++;} } int search(char *str){int len=strlen(str);int root=0;for(int i=0;i<len;i++){int x=str[i]-'a';if(tire[root].next[x]==-1)return 0;root=tire[root].next[x];}return tire[root].sum; } void del(char *str,int word){int len=strlen(str);int root=0;if(word<0)return;for(int i=0;i<len;i++){int x=str[i]-'a';if(tire[root].next[x]==-1)return;tire[root].sum-=word;root=tire[root].next[x];}tire[root].sum=0;for(int i=0;i<26;i++)tire[root].next[i]=-1; }int main(){for(int i=0;i<N;i++)tire[i].init();int t;scanf("%d",&t);while(t--){char str[10],word[35];scanf("%s%s",str,word);if(str[0]=='i')//插入insert(word);else if(str[0]=='d')//刪除del(word,search(word));else{//查詢if(search(word)>0)printf("Yes\n");elseprintf("No\n");}}return 0; }

?

總結

以上是生活随笔為你收集整理的Problem C(HDU-5687)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。