Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define Size 150005
- class Trie {
- int cnt;
- Trie *child[52];
- public:
- Trie() {
- cnt = 0;
- for (int i = 0; i < 52; i++){
- child[i] = NULL;
- }
- }
- void delete_trie() {
- for (int i = 0; i < 52; i++)
- if (child[i] != NULL) {
- child[i]->delete_trie();
- delete child[i];
- }
- }
- void insert_word(string &word, int pos) {
- if (pos == (int)word.length()){
- cnt++;
- return;
- }
- int c;
- if(word[pos] >= 'a') c = (int)(word[pos]-'a')+26;
- else c = (int)(word[pos]-'A');
- if (child[c] == NULL) {
- child[c] = new Trie();
- }
- child[c]->insert_word(word, pos+1);
- }
- int search_word(string &word, int pos) {
- if (pos == (int)word.length()){
- return cnt;
- }
- int c;
- if(word[pos] >= 'a') c = (int)(word[pos]-'a')+26;
- else c = (int)(word[pos]-'A');
- if (child[c] == NULL) {
- return 0;
- }
- return child[c]->search_word(word, pos+1);
- }
- }*root;
- int N,M,len;
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int nCase;
- string s,st,ed,word;
- cin >> nCase;
- for (int cs = 1; cs <= nCase; cs++) {
- cin >> N;
- root = new Trie();
- //Make the dictionary:
- for (int i = 0; i < N; i++) {
- cin >> s;
- if(s.length()>2){
- sort(s.begin()+1,s.end()-1);
- }
- root->insert_word(s,0);
- }
- printf("Case %d:\n",cs);
- //Make sentence:
- cin >> M;
- int res = 1;
- getline(cin,s);
- for (int i = 0; i < M; i++) {
- res = 1;
- getline(cin,s);
- stringstream ss(s);
- while (ss >> word) {
- if(word.length() > 2){
- sort(word.begin()+1,word.end()-1);
- }
- int ret = root->search_word(word,0);
- res = res * ret;
- }
- printf("%d\n",res);
- }
- root->delete_trie();
- delete root;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment