POJ 1816 Wild Words(trie 樹上的DFS)

來源:互聯網
上載者:User

Wild Words
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4377   Accepted: 1142

Description

A word is a string of lowercases. A word pattern is a string of lowercases, '?'s and '*'s. In a pattern, a '?' matches any single lowercase, and a '*' matches none or more lowercases. 

There are many word patterns and some words in your hand. For each word, your task is to tell which patterns match it. 

Input

The first line of input contains two integers N (0 < N <= 100000) and M (0 < M <=100), representing the number of word patterns and the number of words. Each of the following N lines contains a word pattern, assuming all the patterns are numbered from 0 to
N-1. After those, each of the last M lines contains a word. 

You can assume that the length of patterns will not exceed 6, and the length of words will not exceed 20. 

Output

For each word, print a line contains the numbers of matched patterns by increasing order. Each number is followed by a single blank. If there is no pattern that can match the word, print "Not match".

Sample Input

5 4t*?h*s??e**s?*ethistheanis

Sample Output

0 1 3 0 2 4 Not match3

Source

POJ Monthly

個人覺得這個題目是個好題目,但是測試資料有點讓人吐血,有的AC代碼有的測試資料根本通過不了,也能AC

這個題目我糾結了三個多小時,其實程式半小時就搞定了,誰能想到測試資料的範例有重複,而且重複的也要分順序

輸出!開始在trie樹上每次儲存一個,後來用vector儲存多個結果,這才A

解題思路:

  在pattern上建立trie樹,每個節點儲存出現的編號(編號可能不唯一),然後建立完成之後在trie樹上進行搜尋

那麼就OK了

搜尋的時候分三種情況,如果當前位置 ? 可以那麼直接跳過當前節點

如果 *可以,那麼一直跳 0 1 2 3 結束

剩下的正常搜尋

還要注意一種情況就是當字串匹配結束後不能停止,還要判斷後面有沒有全是**的pattern

這個題目是個非常好的題目,測試資料也沒什麼大問題,就是你有重複倒是給個提示撒!

#include <iostream>#include <stdio.h>#include <vector>#include <string.h>#include <algorithm>using namespace std;struct trie{trie *next[28];vector<int> num;}re_root,po[100000];int pos,global_num;int ans[100010];int ans_pos;int init(trie *root){memset(root->next,0,sizeof(root->next));root->num.clear();return 0;}int insert_trie(trie *root,char *name){if(name[0]==0){ root->num.push_back(global_num); global_num++;// return 0;}if(name[0] >='a' && name[0] <='z'){if(root->next[name[0]-'a'])insert_trie(root->next[name[0]-'a'],name+1);else{root->next[name[0]-'a']=&po[pos];init(&po[pos]);pos++;insert_trie(root->next[name[0]-'a'],name+1);}}else{if(name[0]=='?'){if(root->next[26])insert_trie(root->next[26],name+1);else{root->next[26]=&po[pos];init(&po[pos]);pos++;insert_trie(root->next[26],name+1);}}else{if(root->next[27])insert_trie(root->next[27],name+1);else{root->next[27]=&po[pos];init(&po[pos]);pos++;insert_trie(root->next[27],name+1);}}}return 0;}int query(trie *root,char *name){int i,j,k;if(name[0]==0 && !root->num.empty()){for(i=0;i<root->num.size();i++)ans[ans_pos++]=root->num[i];}if(name[0]==0){//root=root->next[27];while(root){if(!root->num.empty()){for(i=0;i<root->num.size();i++)ans[ans_pos++]=root->num[i];}root=root->next[27];}return 0;}if(root->next[27]){for(i=0;name[i];i++){query(root->next[27],name+i);}query(root->next[27],name+i);}if(root->next[26])query(root->next[26],name+1);if(root->next[name[0]-'a'])query(root->next[name[0]-'a'],name+1);return 0;}int n,m;char str[100];bool cmp(const int &a,const int &b){return a < b;}int main(){int i,j,k;while(scanf("%d%d",&n,&m)!=EOF){pos=0;init(&re_root);global_num=1;for(i=0;i<n;i++){//global_num=i+1;scanf("%s",str);insert_trie(&re_root,str);}for(i=0;i<m;i++){ans_pos=0;scanf("%s",str);query(&re_root,str);if(ans_pos==0)printf("Not match");else{sort(ans,ans+ans_pos,cmp);printf("%d",ans[0]-1);for(j=1;j<ans_pos;j++)if(ans[j]!=ans[j-1])printf(" %d",ans[j]-1);}printf("\n");}}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.