當前位置:成語大全網 - 書法字典 - 航電1075中的字典樹問題

航電1075中的字典樹問題

# include & ltiostream & gt

# include & lt字符串& gt

使用命名空間std

const int MAXN = 1000000;

string trans【MAXN】;

int len

typedef結構字典節點{

struct DictreeNode * node【26】;

int鍵;

} * Dictree

網樹根;

void insert_word(字符串str)

{

Dictree current,newnode

current = root

for(int I = 0;我& ltstr . length();i++){

if(當前-》;node【str【I】-‘a‘】!= 0){

當前=當前-& gt;node【str【I】-【a】;

}

否則{

new node =(Dictree)malloc(sizeof(DictreeNode));

for(int j = 0;j & lt26;j++)new node-& gt;node【j】= 0;

當前-& gt;node【str【I】-【a‘】= new node;

current = newnode

當前-& gt;key =-1;

}

}

當前-& gt;key = len-1;

}

int find _ word(string字符串)

{

網樹電流;

current = root

for(int I = 0;我& ltstr . length();i++){

if(當前-》;node【str【I】-‘a‘】!= 0)

當前=當前-& gt;node【str【I】-【a】;

else返回-1;

}

返回電流-& gt;關鍵;

}

int main()

{

字符串tmp,temp

root =(Dictree)malloc(sizeof(DictreeNode));

for(int I = 0;我& lt26;i++)root-》;node【I】= 0;

root-& gt;key =-1;

len = 0;

CIN & gt;& gttmp

if(tmp = =“START“){ }

同時(CIN & gt;& gttmp ){

if(tmp = =“END“)break;

否則{

trans【len】= tmp;

len++;

CIN & gt;& gttmp

插入_字(tmp);

}

}

getline(CIN,tmp);//清除結束後換行

while(getline(CIN,tmp))

temp =““;//初始化溫度

if(tmp = =“START“)繼續;

else if(tmp = =“END“)break;

否則{

for(int I = 0;我& lt= tmp . length();i++){

如果(i & lttmp . length()& amp;& amptmp【I】》=‘a‘amp;& amptmp【I】& lt;=‘z‘)temp . append(1,tmp【I】);

否則{

如果(!temp.empty())

int ans = find _ word(temp);

if(ans & gt;-1)cout & lt& lttrans【ans】;

else cout & lt& lttemp

temp . clear();

}

如果(i & lttmp . length())cout & lt;& lttmp【I】;

}

if(I = = tmp . length())cout & lt;& ltendl

}

}

}

//system(“pause“);

返回0;

}