think:
1 Learning dictionary tree today, when I started my lecture, I felt that the template of dictionary tree was strong. I knocked on two dictionary tree titles in the afternoon. I was curious that this topic was started with static arrays, but not static arrays. Compiling and compiling through sample data is also true. But I always answered wrong. Feeling a little unbalanced, I searched the code from the internet. After knocking down, I feel a little clear about the static dictionary tree stored through the static array, but I still don't know why the initial title is wrong, so I can only record it first. Whining.
Curious curiosity
Time Limit: 2000MS Memory Limit: 65536KB
Problem Description
FF gets a set of n numbers. Don't ask me why, rich, capricious.
FF is curious to know how many digits in the set can be obtained by adding any number before x.
For example, x = 123, the number before x can be 4123, 5123, etc.
Input
Multiple sets of inputs.
For each set of data
First enter n (1 <= n <= 100000).
Next n lines. Each row has a number y (1 <= y <= 100000) representing the elements in the set.
The next line enters m (1 <= m <== 100000), representing m queries.
The next m lines.
Each line has a positive integer x (1 <= x <= 100000).
Output
For each set of data, output a number to represent the answer.
Example Input
3
12345
66666
12356
3
45
12345
356
Example Output
1
0
1
Hint
Author
zmx
Here is the accepted code
#include <stdio.h>
#include <string.h>
int top;
struct node
{
int next[26];
int flag;
int cnt;
}st[6000000];
int Creat()
{
memset(st[top].next, -1, sizeof(st[top].next));
st[top].flag = 0;
st[top].cnt = 0;
top++;
return top-1;
///return top++;
}
void Insert(int rt, char *s)
{
int len = strlen(s);
for(int i = len-1; i >= 0; i--)
{
int t = s[i] - '0';
if(st[rt].next[t] == -1)
{
st[rt].next[t] = Creat();
}
st[rt].flag++;
rt = st[rt].next[t];
}
}
int Search(char *s, int rt)
{
int len = strlen(s);
for(int i = len-1; i >= 0; i--)
{
int t = s[i] - '0';
if(st[rt].next[t] == -1)
{
return 0;
}
rt = st[rt].next[t];
}
return st[rt].flag;
//return st[rt].cnt;
}
int main()
{
int n, m, rt;
char s[14];
while(scanf("%d", &n) != EOF)
{
top = 0;
rt = Creat();
while(n--)
{
scanf("%s", s);
Insert(rt, s);
}
scanf("%d", &m);
while(m--)
{
scanf("%s", s);
printf("%d\n", Search(s, rt));
}
}
return 0;
}
/***************************************************
User name: jk160630
Result: Accepted
Take time: 416ms
Take Memory: 1384KB
Submit time: 2017-02-10 20:47:54
****************************************************/
The following wrong answer code
/* Inverse Order Application of Dictionary Tree*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAXC 10
#define MAXN 600000
typedef struct node
{
int cnt;
int flag;//flag records the number of times a string passes through each node when the string is inserted
struct node *next[MAXC];
}Trie;
Trie trie[MAXN];
int top;
Trie * Build()//Establish node
{
Trie *p = &trie[top++];
p->cnt = 0;
p->flag = 0;
for(int i = 0; i < MAXC; i++)
p->next[i] = NULL;
return p;
}
Trie * Creat()//Initialization
{
top = 0;
return Build();
}
void Insert(Trie *root, char *str)//Insert operation
{
int len = strlen(str);
Trie *p = root;
for(int i = len-1; i >= 0; i--)//(Inverse insertion)
{
int t = str[i] - '0';
if(p->next[t] == NULL)
p->next[t] = Build();
p = p->next[t];
p->flag++;
}
p->cnt++;
}
int Find1(Trie *root, char *str)//Determine whether it is a substring of a string?
{
int len = strlen(str);
Trie *p = root;
for(int i = len-1; i >= 0; i--)
{
int t = str[i] - '0';
if(p->next[t] == NULL)
return p->cnt;
p = p->next[t];
}
return p->cnt;
}
int Find2(Trie *root, char *str)//Get the flag record value of the last character at the node
{
int len = strlen(str);
Trie *p = root;
for(int i = len-1; i >= 0; i--)
{
int t = str[i] - '0';
if(p->next[t] == NULL)
break;
//p->flag++;
p = p->next[t];
//p->flag++;
}
return p->flag;
}
int main()
{
int n, m;
char str[9];
while(scanf("%d", &n) != EOF)
{
Trie *root;
root = Creat();//Initialization + Establishment
while(n--)
{
scanf("%s", str);
Insert(root, str);//String insertion
}
scanf("%d", &m);
while(m--)
{
scanf("%s", str);
if(Find1(root, str) == 0)//Find1 returns 0 if it is a substring of a string
printf("%d\n", Find2(root, str));//Find2 returns the record value flag
else
printf("0\n");
}
}
return 0;
}
/***************************************************
User name: jk160630
Result: Wrong Answer
Take time: 372ms
Take Memory: 1380KB
Submit time: 2017-02-10 19:44:40
****************************************************/