开始不知道gets也像getchar一样会吞scanf的最后字符,导到致一直多读入了一个换行,一直PE。
换了个姿势写三叉树。
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 6000010;
int Node[maxn][3], cnt[maxn];
char elem[maxn];
int top, sstp;
char ss[maxn][12];
int newNode(char ch)
{
top ++;
cnt[top] = Node[top][0] = Node[top][1] = Node[top][2] = 0;
elem[top] = ch;
return top;
}
void insert(char str[])
{
int len = strlen(str);
int p = 1;
for(int i = 0; i < len; i++)
{
while(elem[p] != str[i])
{
if(str[i] < elem[p])
{
if(!Node[p][0])Node[p][0] = newNode(str[i]);
p = Node[p][0];
}
else
{
if(!Node[p][1])Node[p][1] = newNode(str[i]);
p = Node[p][1];
}
}
if(!Node[p][2])Node[p][2] = newNode('\0');
p = Node[p][2];
}
cnt[p] = sstp;
}
int find(char str[])
{
int p = 1;
int len = strlen(str);
for(int i = 0; i < len; i++)
{
while(str[i] != elem[p])
{
if(str[i] < elem[p])
if(!Node[p][0])return 0;
else p = Node[p][0];
else
if(!Node[p][1])return 0;
else p = Node[p][1];
}
if(!Node[p][2])return 0;
else p = Node[p][2];
}
return cnt[p];
}
int main()
{
char str[15];
char tmp[3004];
sstp = 1;
scanf("%s",str);
top = 0;
newNode('\0');
while(scanf("%s",ss[sstp]))
{
if(ss[sstp][0] == 'E')break;
scanf("%s",str);
insert(str);
sstp ++;
}
getchar();
gets(tmp);
while(1)
{
gets(tmp);
if(tmp[0] == 'E')break;
int i,len, t;
len = strlen(tmp);
t = 0;
for(i = 0; i < len; i++)
{
if(tmp[i] < 'a' || tmp[i] > 'z')
{
str[t] = '\0';
t = 0;
sstp = find(str);
if(sstp)printf("%s",ss[sstp]);
else printf("%s", str);
printf("%c",tmp[i]);
}
else str[t++] = tmp[i];
}
printf("\n");
}
return 0;
}