unfinished

#include<cstdio>
#include <cstdlib>
#include <stdio.h>
#include<sstream>
#include<malloc.h>
#include<string>
#include<cstring>
#include<iostream>
using namespace std;

struct Node
{
struct Node *child[10];
}root;
struct Node *start;

void init(struct Node *node)
{
//node=(struct Node*)malloc(sizeof(struct Node));
for(int i=0;i<10;i++)
{
node->child[i]=NULL;
}
}

void insert(char *word)
{
int len=strlen(word);
struct Node *Now=&root;
for(int i=0;i<len;i++)
{
if(Now->child[word[i]-'0']==NULL)
{
struct Node *NewNode=(struct Node*)malloc(sizeof(struct Node));//NewNode不能定义为变量
//要定义为指针,重新分配空间。否则Now指针一直指向同一个空间
init(NewNode);
//NewNode->n=1;
Now->child[word[i]-'0']=NewNode;
Now=NewNode;
}

}

}

int find(char *s,char *word,int pos,struct Node *curr)
{
int lens=strlen(s);
// int lenw=strlen(word);
// if((lenw-pos)<lens)
// return 510000;
int cnt=0;
struct Node *tmp=curr;
struct Node *poss=curr;
for(int i=0;i<lens;i++)
{
if(tmp->child[s[i]-'0']==NULL)
{
cnt++;
}
for(int j=0;j<10;j++)
if(tmp->child[word[j]-'0']!=NULL)
tmp=tmp->child[word[j]-'0'];
// tmp=tmp->child[word[pos]-'0'];
//pos++;
}
for(i=0;i<10;i++)
if(poss->child[word[i]-'0']!=NULL)
{
curr=poss->child[word[i]-'0'];
start=curr;
break;
}
return cnt;
}

void del(struct Node *t) //删除Trie树,并释放空间
{
for(int i=0;i<26 ;i++)
{
if(t->child[i] != NULL)
{
del(t->child[i]) ;
}
}
free(t);
}

char s1[50005];
char s2[50005];


int main()
{
int k=1;
string tt;
int t,i,j;
int len;
int min;
int tmp;
cin>>t;
while(t--)
{
min=510000;
init(&root);
scanf("%s",&s1);
scanf("%s",&s2);
insert(s1);
start=&root;
len=strlen(s1);
for(i=0;i<len;i++)
{
tmp=find(s2,s1,i,start);
if(tmp<min)
min=tmp;
}
cout<<min<<endl;
// del(&root);
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值