hdu 2846 Repository 字典樹的變形

           Repository

            Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/65536 K (Java/Others)
                  Total Submission(s): 1129????Accepted Submission(s): 382


Problem Description
When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.

Input
There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.

Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.

Sample Input
20
ad
ae
af
ag
ah
ai
aj
ak
al
ads
add
ade
adf
adg
adh
adi
adj
adk
adl
aes
5
b
a
d
ad
s

Sample Output
0
20
11
11
2
根據題意可知:題目屬于判斷字符串中是否包含子串的問題,對于一般的字典樹,用來判斷前綴,而這里不能直接這么去建樹。在建樹的時候將字符串X=X1X2....Xn的分別以X1,X2....Xn開頭的后綴子串插入到Trie樹中,如此一來就可以判斷某個字符串是否被包含在另一個字符串當中。但是這就有一個問題,比如插入了字符串abab,那么當查找字符串ab時就會重復計數,因此需要多設計一個標識以表示在插入"abab"和"ab"時時同一個字符串即可(是同一個字符串就不需要使計數器加1),因此在Trie樹結點中多設計一個商品id來標記。id用來記錄最后一個經過此路徑上的商品編號,如果要插入的字符串編號同當前節點的編號不同,則計數器加1,并且將當前結點的編號置為要插入的字符串的編號。
/*hdu 2846 字典樹的變形 2011.10.18*/ 
#include <iostream>
#include<cstring>
#define MAX 26
using namespace std;

typedef struct Trie_Node
{
int count; //記錄包含該結點的單詞個數
int id; //最后一次經過此結點的商品ID
Trie_Node *next[MAX];
}Trie;

void insert(Trie *root,char *s,int id)
{
Trie *p=root;
while(*s!='\0')
{
if(p->next[*s-'a']==NULL)
{
Trie *temp=(Trie *)malloc(sizeof(Trie));
for(int i=0;i<MAX;i++)
{
temp->next[i]=NULL;
}
temp->count=0;
temp->id=-1; //-1表示沒有商品
p->next[*s-'a']=temp;
}
p=p->next[*s-'a'];
if(p->id!=id) //如果當前結點的商品ID不等于要插入商品的ID,則計數器count++,并且重新置ID的值
{
p->id=id;
p->count++;
}
s++;
}
}


int search(Trie *root,char *s)
{
Trie *p=root;
for(int i=0;s[i]!='\0';i++)
{
if(p->next[s[i]-'a']==NULL)
return 0;
p=p->next[s[i]-'a'];
}
return p->count;
}

int main(int argc, char *argv[])
{
int i,j;
int n,m;
char s[21];
Trie *root=(Trie *)malloc(sizeof(Trie));
for(i=0;i<MAX;i++)
{
root->next[i]=NULL;
}
root->count=0;
root->id=-1;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",s);
for(j=0;j<strlen(s);j++) //將字符串X=X1X2...Xn的分別以X1,X2...Xn開頭的后綴字符串插入到Trie樹中
{
insert(root,s+j,i);
}
}
scanf("%d",&m);
for(i=0;i<m;i++)
{
scanf("%s",s);
printf("%d\n",search(root,s));
}
return 0;
}
嘗試過用KMP去解決,但是查找復雜度至少為0(p*(len1+len2)),試著提交了一下,嚴重超時。

轉載于:https://www.cnblogs.com/dolphin0520/archive/2011/10/18/2216208.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378064.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378064.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378064.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

怎樣看虛擬主機的服務器,虛擬主機怎么查看服務器類型

虛擬主機怎么查看服務器類型 內容精選換一換使用華為云提供的公共鏡像制作私有鏡像時&#xff0c;您需先購買云主機等云資源時鏡像選擇公共鏡像、云服務器類型建議統一選擇“s3 (通用計算型)”&#xff0c;在云主機安裝部署完商品&#xff0c;然后參照以下方式進行私有鏡像制作…

Win32動態庫 Lib文件哪去了

最近使用SQLite&#xff0c;用源文件.c和.h編譯SQLite的動態庫&#xff0c;編譯后發現沒有Lib文件。 原來&#xff1a;SQLite的.c文件沒有引用.h文件&#xff0c;添加引用&#xff0c;編譯&#xff0c;Lib文件有了。轉載于:https://www.cnblogs.com/yunuoyuhan/p/3204457.html

console java_Java Console format()方法與示例

console java控制臺類format()方法 (Console Class format() method) format() method is available in java.io package. format()方法在java.io包中可用。 format() method is used to write the formatted string to this Console with the help of the given string format…

Anaconda自帶Python編譯器Jupyter Notebook顯示代碼行數

ESC&#xff1a;進入命令行模式&#xff1b;按下H即可顯示各種快捷鍵信息 Enter&#xff1a;進入編輯模式 方法一&#xff1a;命令方法 一、點擊代碼段&#xff0c;按ESC&#xff0c;使代碼段顯示藍色&#xff0c;進入命令行模式 二、按下ShiftL&#xff0c;顯示代碼行數 方法…

ajax 服務器響應,ajax-服務器響應

如果需要獲得了來自服務器的響應&#xff0c;則使用XMLHttpRequest 對象的 responseText 或 responseXML 屬性responseText&#xff1a;獲得字符串形式的響應數據&#xff0c;當readyState屬性值變為4時&#xff0c;responseText屬性才可用&#xff0c;表明Ajax請求已經結束例&…

(轉)MOMO的Unity3D研究院之深入理解Unity腳本的執行順序(六十二)

http://www.xuanyusong.com/archives/2378 Unity是不支持多線程的&#xff0c;也就是說我們必須要在主線程中操作它&#xff0c;可是Unity可以同時創建很多腳本&#xff0c;并且可以分別綁定在不同的游戲對象身上&#xff0c;他們各自都在執行自己的生命周期感覺像是多線程&…

SQL/MongoDB 連接并發測試

最近一直在搞mongodb 文件服務器大量文件并發上傳測試&#xff0c;在官方文檔發現mongo是線程安全的&#xff0c;支持單一連接下的并發操作。印象ADO.NET 似乎不支持單一連接并發。于是&#xff0c;測試一下來證實這個疑慮。&#xff08;前兩篇小記一直糾結mongodb吃內存導致并…

【C、C++基礎】什么時候用 “.” 什么時候用“->”(3個實例搞懂)

從堆棧的角度來說&#xff1a; 從堆棧的角度來說&#xff1a; 對象放在堆上&#xff0c;就要用指針&#xff0c;也就是對象指針->函數&#xff1b; 放在棧上,就對象.函數 那么如何判斷對象放在堆上還是棧上&#xff1f; 從我的另一篇筆記【C grammar】C簡化內存模型可知&am…

java clone方法_Java Calendar clone()方法與示例

java clone方法日歷類clone()方法 (Calendar Class clone() method) clone() method is available in java.util package. clone()方法在java.util包中可用。 clone() method is used to return the cloned object of this Calendar object. clone()方法用于返回此Calendar對象…

三、Numpy數組操作

一、對圖片各個像素點的像素值進行操作 image.shape[0]&#xff1a;image圖像的height image.shape[1]&#xff1a;image圖像的width image.shape[2]&#xff1a;image圖像的channels import cv2 import numpy as npdef access_pixels(image):print(image.shape)height imag…

picacg服務器維護,picacg的服務器地址是什么

彈性云服務器 ECS彈性云服務器(Elastic Cloud Server)是一種可隨時自助獲取、可彈性伸縮的云服務器&#xff0c;幫助用戶打造可靠、安全、靈活、高效的應用環境&#xff0c;確保服務持久穩定運行&#xff0c;提升運維效率三年低至5折&#xff0c;多種配置可選了解詳情用戶數據注…

Redis-Sampler:深入了解你的Redis存儲

redis-sampler 是Redis作者antirez 同學開發的一個ruby 小工具&#xff0c;用于對Redis存儲概況進行抽樣檢測并給出分析結果。 項目地址&#xff1a;https://github.com/antirez/redis-sampler 使用方式&#xff1a; 下載源碼&#xff0c;執行下面命令&#xff1a; ./redis-sam…

二叉樹筆記(深度遍歷與廣度遍歷+13道leetcode題目(深度3道、廣度10道))

本文章為結合leetcode題目以及公眾號“代碼隨想錄”的文章所做的筆記&#xff01; 感覺代碼隨想錄的題目整理真的很好&#xff0c;比自己盲目刷題好很多。 目錄1、二叉樹小記1、滿二叉樹與完全二叉樹2、二叉搜索樹3、平衡二叉搜索樹AVL4、二叉樹存儲方式5、二叉樹遍歷方式6、二…

ZZ的計算器

Problem Description ZZ自從上大學以來&#xff0c;腦容量就是以SB計算的&#xff0c;這個吃貨竟然連算術運算也不會了&#xff0c;可是當今的計算機可是非常強大的&#xff0c;作為ACMer&#xff0c; 幾個簡單的算術又算得了什么呢&#xff1f;可是該怎么做呢&#xff1f;ZZ只…

kotlin 覆蓋屬性_Kotlin程序| 方法覆蓋的示例

kotlin 覆蓋屬性方法重載 (Method Overriding) Method overriding allows derived class has the same function name and signature as the base class 方法重寫允許派生類具有與基類相同的函數名稱和簽名 By method overriding we can provide different implementation into…

對視頻中的特征顏色物體(青色水杯)進行跟蹤

方法一&#xff1a;目標物體白色&#xff0c;其余黑色 import cv2 import numpy as npdef extrace_object():capture cv2.VideoCapture("G:/Juptyer_workspace/study/data/yy.mp4")while(True):ret,frame capture.read()if retFalse:breakhsv cv2.cvtColor(frame…

Android實現號碼歸屬地查詢

我們通過發送XML訪問 WebService就可以實現號碼的歸屬地查詢&#xff0c;我們可以使用代理服務器提供的XML的格式進行設置&#xff0c;然后請求提交給服務器&#xff0c;服務器根據請求就會返回給一個XML&#xff0c;XML中就封裝了我們想要獲取的數據。 發送XML 1.通過URL封裝路…

如何從 Datagrid 中獲得單元格的內容與 使用值轉換器進行綁定數據的轉換IValueConverter...

一、如何從 Datagrid 中獲得單元格的內容 DataGrid 屬于一種 ItemsControl, 因此&#xff0c;它有 Items 屬性并且用ItemContainer 封裝它的 items. 但是&#xff0c;WPF中的DataGrid 不同于Windows Forms中的 DataGridView。 在DataGrid的Items集合中&#xff0c;DataGridRow…

【C++ grammar】常量、指針、Usage of using, typedef, and #define

目錄1、常量 &#xff08;Constant&#xff09;2、指針&#xff08;Pointer&#xff09;3、Usage of using, typedef, and #define1、常量 &#xff08;Constant&#xff09; 常量是程序中一塊數據&#xff0c;這個數據一旦聲明后就不能被修改了。 如果這塊數據有一個名字&am…

斯威夫特山地車_斯威夫特| 兩個數字相加的程序

斯威夫特山地車In this program, we will have an idea - how two numbers can be added and displayed as the output on the screen? 在此程序中&#xff0c;我們將有一個想法- 如何將兩個數字相加并顯示為屏幕上的輸出 &#xff1f; Open XCode terminal and type the fol…