1. 程式人生 > >全文檢索-hdu1277

全文檢索-hdu1277

全文檢索

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


Problem Description 我們大家經常用google檢索資訊,但是檢索資訊的程式是很困難編寫的;現在請你編寫一個簡單的全文檢索程式。問題的描述是這樣的:給定一個資訊流檔案,資訊完全有數字組成,數字個數不超過60000個,但也不少於60個;再給定一個關鍵字集合,其中關鍵字個數不超過10000個,每個關鍵字的資訊數字不超過60個,但也不少於5個;兩個不同的關鍵字的前4個數字是不相同的;由於流檔案太長,已經把它分成多行;請你編寫一個程式檢索出有那些關鍵字在檔案中出現過。  

 

Input 第一行是兩個整數M,N;M表示數字資訊的行數,N表示關鍵字的個數;接著是M行資訊數字,然後是一個空行;再接著是N行關鍵字;每個關鍵字的形式是:[Key No. 1] 84336606737854833158。  

 

Output 輸出只有一行,如果檢索到有關鍵字出現,則依次輸出,但不能重複,中間有空格,形式如:Found key: [Key No. 9] [Key No. 5];如果沒找到,則輸出形如:No key can be found !。  

 

Sample Input 20 10 646371829920732613433350295911348731863560763634906583816269 637943246892596447991938395877747771811648872332524287543417 420073458038799863383943942530626367011418831418830378814827 679789991249141417051280978492595526784382732523080941390128 848936060512743730770176538411912533308591624872304820548423 057714962038959390276719431970894771269272915078424294911604 285668850536322870175463184619212279227080486085232196545993 274120348544992476883699966392847818898765000210113407285843 826588950728649155284642040381621412034311030525211673826615 398392584951483398200573382259746978916038978673319211750951 759887080899375947416778162964542298155439321112519055818097 642777682095251801728347934613082147096788006630252328830397 651057159088107635467760822355648170303701893489665828841446 069075452303785944262412169703756833446978261465128188378490 310770144518810438159567647733036073099159346768788307780542 503526691711872185060586699672220882332373316019934540754940 773329948050821544112511169610221737386427076709247489217919 035158663949436676762790541915664544880091332011868983231199 331629190771638894322709719381139120258155869538381417179544 000361739177065479939154438487026200359760114591903421347697   [Key No. 1] 934134543994403697353070375063 [Key No. 2] 261985859328131064098820791211 [Key No. 3] 306654944587896551585198958148 [Key No. 4] 338705582224622197932744664740 [Key No. 5] 619212279227080486085232196545 [Key No. 6] 333721611669515948347341113196 [Key No. 7] 558413268297940936497001402385 [Key No. 8] 212078302886403292548019629313 [Key No. 9] 877747771811648872332524287543 [Key No. 10] 488616113330539801137218227609  

 

Sample Output Found key: [Key No. 9] [Key No. 5]     題目的大致意思: 給定一給比較長的字串(字串僅由數字組成),長的字串被拆成了M行。然後輸入N個匹配的字串(我們簡稱Key字串), 讓你輸出在長字串中找有那些Key字串,並按照出現的順序依次輸出。(每個Key字串僅輸出一次)     我的解題過程: 開始是想用字典樹寫的,畢竟這題也算是字典樹的模板題了。但是,我還是太水了(字典樹不太會)。其實是怕超記憶體了。。。
然後就想可不可以用KMP解決,但是又怕超時。所以想了一個比較蠢的方法——應該是暴力。(手動滑稽)   解題思路: 由於文中說明了每個Key字串的前4位都不相同。所以我就想以它們的前4位建立string陣列。並將它們的前4位作為陣列下標存入。 同時建立sign陣列,對映它們的i值 然後依次取長字串的4位進行比較。如果比較成功且sign值不為0,就將編號存入vector內,並將sign[前4位]=0。    

AC程式碼:

 

 1 #include<iostream>
 2 #include<vector>
 3 #include<string>
 4 using namespace std;
 5 const int MAX = 60000;
 6 const int MAX_NUM = 10000;
 7 char store[MAX + 10];
 8 char temp[100];
 9 int sign[MAX_NUM];             //sign[i]記錄的是target的下標
10 string target[MAX_NUM];
11 vector<int> ans;
12 void insearch(char *set,string &s,int num)
13 {
14     int length = s.length();
15     for (int i = 0; i < length; ++i) {
16         if (set[i] != s[i]) {
17             return;
18         }
19     }
20     if (sign[num] != 0) {
21         ans.push_back(sign[num]);
22         sign[num] = 0;
23     }
24 }
25 int main()
26 {
27     int m = 0, n = 0, length = 0;
28     cin >> m >> n;
29     getchar();
30     while (m--) {
31         while ((store[length] = getchar()) != '\n') ++length;
32     }
33     store[length] = '\0';
34     getchar();
35     for (int i = 1; i <= n; ++i) {
36         int l = 0;
37         while ((temp[l] = getchar()) != '\n') {
38             ++l;
39         }
40         temp[l] = '\0';
41         int cnt = 0, p = i;
42         while (p >= 10) {
43             ++cnt;
44             p /= 10;
45         }
46         int num = (temp[12+cnt] - '0') * 1000 + (temp[13+cnt] - '0') * 100 + (temp[14+cnt] - '0') * 10 + (temp[15+cnt] - '0');//取出前4位
47         for (int i = 16+cnt; i < l; ++i) {
48             target[num] += temp[i];
49         }
50         sign[num] = i;//對映i值
51     }
52     int num = (store[0] - '0') * 100 + (store[1] - '0') * 10 + (store[2] - '0');//依次取出前4位進行比較
53     for (int i = 3; i < length; ++i) {
54         num = (num % 1000) * 10 + (store[i] - '0');
55         if(sign[num]) insearch(store + i + 1, target[num], num);
56     }
57     if (ans.size()) {
58         int size = ans.size() - 1;
59         printf("Found key: ");
60         for (int i = 0; i < size; ++i) {
61             printf("[Key No. %d] ", ans[i]);
62         }
63         printf("[Key No. %d]\n", ans[size]);
64     }
65     else {
66         printf("No key can be found !\n");
67     }
68 }