我就是喜欢敲trie树


#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
#include <string>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

#define pi 3.1415926535897932385
#define LL64 __int64
#define LL long long
#define oo 2147483647
#define N 200000
#define M 2050

char x[15], y[15], str[3030];

class trie
{
public:
int pc;
int trie_node[N][26];
char word[N][15];
void init(int i)
{
memset(trie_node[i], 0, sizeof(trie_node[i]));
}
void insert()
{
int p = 0;
int j = 0;
while (y[j] != '\0')
{
if (trie_node[p][y[j] - 'a'] == 0)
{
pc++;
init(pc);
trie_node[p][y[j] - 'a'] = pc;
p = pc;
}
else
{
p = trie_node[p][y[j] - 'a'];
}
j++;
}
strcpy(word[p], x);
}
void search(int len)
{
int p = 0;
for (int i = 0; i < len; i++)
{
if (trie_node[p][x[i] - 'a'] == 0)
{
printf("%s", x);
return;
}
else p = trie_node[p][x[i] - 'a'];
if (i == len - 1) printf("%s", word[p]);
}
}
}t;

int main()
{
while (~scanf("%s", x))
{
t.init(0);
t.pc = 0;
while (scanf("%s", x))
{
if (strcmp(x, "END") == 0) break;
scanf("%s", y);
t.insert();
}

for (int i = 0; i < t.pc; i++)
{
for (int j = 0; j < 26; j++)
{
printf("%2d", t.trie_node[i][j]);
}
printf("\n");
}*/
scanf("%s", x);
getchar();
while (gets(str))
{
if (strcmp(str, "END") == 0) break;
int len = strlen(str);
int l = 0;
for (int i = 0; i < len; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
x[l] = str[i];
l++;

if (i == len - 1){x[i] = '\0'; t.search(l);}
}
else
{
x[l] = '\0';
if (l) t.search(l);
printf("%c", str[i]);
l = 0;
}
}
printf("\n");
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值