Download spoj_cpp_clean/ADAINDEX/ADAINDEX-9.cpp from slayone/uva_spoj_raw: direct link, hf CLI and curl.
- Browser
- Download file 2.31 kB
-
https://huggingface.co/datasets/slayone/uva_spoj_raw/resolve/main/spoj_cpp_clean/ADAINDEX/ADAINDEX-9.cpp
- Command line
-
hf download hf://datasets/slayone/uva_spoj_raw/spoj_cpp_clean/ADAINDEX/ADAINDEX-9.cpp
-
curl -L -o ADAINDEX-9.cpp https://huggingface.co/datasets/slayone/uva_spoj_raw/resolve/main/spoj_cpp_clean/ADAINDEX/ADAINDEX-9.cpp
2.31 kB
| /* | |
| **************************************************************** | |
| **************************************************************** | |
| -> Coded by Stavros Chryselis | |
| -> Visit my github for more solved problems over multiple sites | |
| -> https://github.com/StavrosChryselis | |
| -> Feel free to email me at stavrikios@gmail.com | |
| **************************************************************** | |
| **************************************************************** | |
| */ | |
| using namespace std; | |
| struct ver | |
| { | |
| int words; | |
| int prefixes; | |
| struct ver **edges; | |
| }; | |
| void initialize(ver **vertex) | |
| { | |
| int i; | |
| (*vertex) = (struct ver*)malloc(sizeof(struct ver)); | |
| (*vertex)->words = 0; | |
| (*vertex)->prefixes = 0; | |
| (*vertex)->edges = (struct ver**)malloc(sizeof(ver) * 26); | |
| for (i = 0; i<26; i++) | |
| (*vertex)->edges[i] = NULL; | |
| } | |
| void addword(ver **vertex, string &word, int pos = 0) | |
| { | |
| if (pos == word.size()) | |
| { | |
| (*vertex)->words++; | |
| (*vertex)->prefixes++; | |
| return; | |
| } | |
| char k = word[pos]; | |
| (*vertex)->prefixes++; | |
| if ((*vertex)->edges[k - 'a'] == NULL) | |
| initialize(&(*vertex)->edges[k - 'a']); | |
| addword(&(*vertex)->edges[k - 'a'], word, pos + 1); | |
| } | |
| int countwords(ver *vertex, string &word, int pos = 0) | |
| { | |
| if (pos == word.size()) | |
| return vertex->words; | |
| char k = word[pos]; | |
| if (vertex->edges[k - 'a'] == NULL) | |
| return 0; | |
| return countwords(vertex->edges[k - 'a'], word, pos + 1); | |
| } | |
| int countprefixes(ver *vertex, string &word, int pos = 0) | |
| { | |
| if (pos == word.size()) | |
| return vertex->prefixes; | |
| char k = word[pos]; | |
| if (vertex->edges[k - 'a'] == NULL) | |
| return 0; | |
| return countprefixes(vertex->edges[k - 'a'], word, pos + 1); | |
| } | |
| inline void next_string(string &A) | |
| { | |
| A.clear(); | |
| char c = gc(); | |
| while (isspace(c)) | |
| c = gc(); | |
| while (!isspace(c) && c != EOF) | |
| { | |
| A.push_back(c); | |
| c = gc(); | |
| } | |
| } | |
| struct ver *trie; | |
| int N, Q; | |
| inline void init() | |
| { | |
| string S; | |
| scanf("%d %d", &N, &Q); | |
| initialize(&trie); | |
| while (N--) | |
| { | |
| next_string(S); | |
| addword(&trie, S); | |
| } | |
| } | |
| inline int query() | |
| { | |
| string S; | |
| next_string(S); | |
| return countprefixes(trie, S); | |
| } | |
| int main() | |
| { | |
| // freopen("input.txt", "r", stdin); | |
| init(); | |
| while (Q--) | |
| printf("%d\n", query()); | |
| return 0; | |
| } | |