1. 程式人生 > >天天寫演算法之(字典樹)T9

天天寫演算法之(字典樹)T9

腦殼疼,腦殼疼,哎,寫程式碼幾分鐘,debug一小時。還是要說說,指標的問題。指標的傳遞,我感覺吃了一點虧。其中最讓我不明白的地方是。

如果一個結構體例內的建構函式,不能賦值給一個物件。

struct temp {
    int p;
    temp()
    {
        p = 0;
    }
}tp;
如果寫下面程式碼會報錯:
tp = new temp();
出現的錯誤是這個
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
E:\codeblock\nameSpace\t\main.cpp||In function 'int main()':|
E:\codeblock\nameSpace\t\main.cpp|71|error: no match for 'operator=' (operand types are 'temp' and 'temp*')|
E:\codeblock\nameSpace\t\main.cpp|63|note: candidate: temp& temp::operator=(const temp&)|
E:\codeblock\nameSpace\t\main.cpp|63|note:   no known conversion for argument 1 from 'temp*' to 'const temp&'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
emmm,我不是很明白,如果有大佬知道的話,麻煩說一下,我就把這個先記下來。ac程式碼:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std ;

int Num_key[10]={0,0,3,3,3,3,3,4,3,4};
string res_str ;
int res_sum ;
string input ;
string keys[10]={"*","*","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
struct Node{
    Node *next[26];
    bool isWord;
    int fruq;
    Node(){
        for(int i = 0 ; i < 26 ; i ++)
            next[i]=NULL;
        isWord = false;
        fruq = 0 ;
    }
}*root;

void insert_Node(string target , int fruq)
{
    Node *p ;
    p = root ;
    for(int i =0 ; i <target.length();i++)
    {
        if(p->next[target[i]-'a']==NULL)
        {
                p->next[target[i]-'a'] = new Node();
        }
        p = p->next[target[i]-'a'] ;
        p->fruq += fruq;
    }
     p->isWord = true ;
}


void dfs(int current ,int target_length ,Node *node,string res)
{

    if(current == target_length)
    {
        if(node->fruq>res_sum)
        {
            res_sum = node->fruq;
            res_str = res ;
        }
        //return ;
    }
    int t = input[current]-'0';

    for(int i = 0 ; i <Num_key[t];i++ )
    {
        if(node->next[keys[t][i]-'a']!=NULL)
        {
            dfs(current +1 , target_length, node->next[keys[t][i]-'a'],res+keys[t][i]);
        }
    }
}

int main(){

    int T ,t,fruq;
    string  target ;
    scanf("%d",&T);
    int index= 1 ;
    while(T--)
    {
        printf("Scenario #%d:\n",index ++);

        root = new Node();
        scanf("%d",&t);
        while(t--)
        {
            cin>>target;
            cin>>fruq;
            insert_Node(target,fruq);
        }
         scanf("%d",&t);
         while(t--)
         {
            cin>>input;
            int len = input.length();
            for(int i=1;i<len ; i ++)
            {
                res_sum = 0 ;
                dfs(0,i,root,"");
                if(res_sum)
                    cout <<res_str<<endl ;
                else
                    cout <<"MANUALLY"<<endl;

            }
            cout <<endl ;
         }
         cout <<endl ;
    }
    return 0 ;

}