Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct Trie{
- int nr;
- struct Trie *v[27];
- };
- typedef struct Trie Trie;
- void Init(Trie **t){
- int i;
- (*t) = (Trie*)malloc(sizeof(Trie));
- (*t) -> nr = 0;
- for(i = 0; i < 27; ++i){
- (*t) -> v[i] = 0;
- }
- }
- void Insert(Trie *t, char *s){
- if(*s == '\0'){
- t -> nr++;
- }
- else{
- int poz = *s - 'a';
- if(t -> v[poz] == NULL){
- Init(&t -> v[poz]);
- }
- Insert(t -> v[poz], s + 1);
- }
- }
- int Query(Trie *t, char *s){
- if(*s == '\0'){
- return t -> nr;
- }
- else{
- int poz = *s - 'a';
- if(t -> v[poz] != NULL){
- return Query(t -> v[poz], s + 1);
- }
- else{
- return 0;
- }
- }
- }
- int main()
- {
- freopen("date.in", "r", stdin);
- freopen("date.out", "w", stdout);
- int n, i;
- char s[20];
- Trie *root;
- Init(&root);
- scanf("%d", &n);
- for(i = 0; i < n; ++i){
- scanf("%s", s);
- Insert(root, s);
- }
- scanf("%d", &n);
- for(i = 0; i < n; ++i){
- scanf("%s", s);
- printf("%d\n", Query(root, s));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement