zoukankan      html  css  js  c++  java
  • 字典树 + 深搜 之 hdu 1298

    //  [7/16/2014 Sjm]
    /*
    字典树 + 深搜。。。
    (好久没碰搜索了,调试了一段时间,不过还好1A。。。)

    */

      1 #include <iostream>
      2 #include <cstdlib>
      3 #include <cstdio>
      4 #include <cstring>
      5 #include <algorithm>
      6 #include <map>
      7 #include <string>
      8 #include <functional>
      9 using namespace std;
     10 const int MAX = 26;
     11 
     12 map<int, string> my_map;
     13 string ans_str;
     14 int max_val;
     15 bool myJudge;
     16 
     17 struct Trie{
     18     int val;
     19     Trie* next[MAX];
     20     Trie(){
     21         val = 0;
     22         memset(next, NULL, sizeof(next));
     23     }
     24 };
     25 
     26 Trie* Root;
     27 
     28 void CreTrie(char* str, int t_val) {
     29     int len = strlen(str);
     30     Trie* p = Root;
     31     for (int i = 0; i < len; i++) {
     32         int pos = str[i] - 'a';
     33         if (!(p->next[pos])) {
     34             p->next[pos] = new Trie;
     35         }
     36         p->next[pos]->val += t_val;
     37         p = p->next[pos];
     38     }
     39 }
     40 
     41 void DelTrie(Trie* T)
     42 {
     43     for (int i = 0; i < MAX; i++) {
     44         if (T->next[i]) {
     45             DelTrie(T->next[i]);
     46         }
     47     }
     48     delete[] T;
     49 }
     50 
     51 void Dfs_Trie(char* str, int pos, Trie* p, string for_ans_str)
     52 {
     53     if (strlen(str) == pos) {
     54         myJudge = true;
     55         if (p->val > max_val) {
     56             ans_str = for_ans_str;
     57             max_val = p->val;
     58         }
     59         return;
     60     }
     61     int key = str[pos] - '0';
     62     int len = my_map[key].size();
     63     for (int i = 0; i < len; i++) {
     64         int now_node = my_map[key][i] - 'a';
     65         if (!(p->next[now_node])) {
     66             continue;
     67         }
     68         else {
     69             Dfs_Trie(str, pos + 1, p->next[now_node], for_ans_str + my_map[key][i]);
     70         }
     71     }
     72 }
     73 
     74 int main()
     75 {
     76     my_map[2] = "abc";    my_map[3] = "def";
     77     my_map[4] = "ghi";    my_map[5] = "jkl";
     78     my_map[6] = "mno";    my_map[7] = "pqrs";
     79     my_map[8] = "tuv";    my_map[9] = "wxyz";
     80     //freopen("input.txt", "r", stdin);
     81     //freopen("output.txt", "w", stdout);
     82     int N;
     83     scanf("%d", &N);
     84     char str[105], num[105];
     85     int w, m, val;
     86     for (int i = 1; i <= N; i++) {
     87         printf("Scenario #%d:
    ", i);
     88 
     89         Root = new Trie;
     90 
     91         scanf("%d", &w);
     92         while (w--) {
     93             scanf("%s %d", str, &val);
     94             CreTrie(str, val);
     95         }
     96 
     97         scanf("%d", &m);
     98         while (m--){
     99             scanf("%s", num);
    100             int len = strlen(num) - 1;
    101             char t_str[105] = { '' };
    102             for (int j = 1; j <= len; j++) {
    103                 strncpy(t_str, num, j);
    104                 myJudge = false;
    105                 max_val = 0;
    106                 Dfs_Trie(t_str, 0, Root, "");
    107                 if (!myJudge) { printf("MANUALLY
    "); }
    108                 else { cout << ans_str << endl; }
    109             }
    110             printf("
    ");
    111         }
    112         printf("
    ");
    113         DelTrie(Root);
    114     }
    115     return 0;
    116 }
  • 相关阅读:
    R、Python、Scala 和 Java,到底该使用哪一种大数据编程语言?
    iOS7
    The “Experimental” status of Multipath TCP
    (OK) porting MPTCP to LineageOS-14.1-kiwi (Android-7.1.1,运行在Huawei honor 5x) for VirtualBox- 100% 成功
    ip_route_output_key函数分析(1)
    (OK) porting MPTCP to LineageOS-14.1-kiwi (Android-7.1.1,运行在Huawei honor 5x) for VirtualBox
    (2) linux 3.x
    【CodeForces 271D】Good Substrings
    【CodeForces 987C】Three displays
    【CodeForces 574B】Bear and Three Musketeers
  • 原文地址:https://www.cnblogs.com/shijianming/p/4140831.html
Copyright © 2011-2022 走看看