Find words

I don't know where my code went wrong, I got two correct answers and 8 wrong answers, thanks in advance for any help.

A word is defined as a string of w characters from a alphabet S.

A document is defined as a sequence of words.

Given n documents D:=(Di)i=1~n where each document is of mi words.

Document counting is to count number of documents where a word occurs when the word is given.

There will be document counting queries.

Input Format

The 1st line is n.
The following 2n lines are documents, where the 1st and 2nd line of every 2 lines are mi and Di.
The following line is q .
The following q lines are words for document counting query.

Output Format

Output m lines of response for document counting queries.

Sample Input 0

5
2
A AB
1
A
3
A AB ABC
5
A AB ABC ABCD ABCDE
4
A AB ABC ABCD
3
AB
C
ABCDE
Sample Output 0

4
0
1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <unordered_map>
#include <map>
using namespace std;


int main() {
   uint64_t n;
    cin>>n;
    map<string, uint64_t> table;
    while(n--)
    {
        uint64_t a;
        cin>>a;
        while(a--)
        {
            string N;
            cin >> N;
            table[N]++; 
        }
    }
   uint64_t q;
   cin>>q;
    while (q--)
  {
    string key;
    cin >> key;
    if (table.find(key) != table.end()) { cout << table[key] << "\n"; }
    else                                { cout << "0\n"; }
  }
  
    return 0;
}
Last edited on
Well, it will fail if any word appears more than once in a given document.
Yes I do see that problem too, thank you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;

int main()
{
   int n;
   cin >> n;

   map<string,int> freq;
   while ( n-- )
   {
      int m;
      cin >> m;

      // Find distinct words in this dictionary
      set<string> S;
      while ( m-- )
      {
         string word;
         cin >> word;
         S.insert( word );
      }

      // Put the distinct words only in the frequency table
      for ( const string &str : S ) freq[str]++;
   }

   // Answer queries
   int q;
   cin >> q;
   while ( q-- )
   {
      string str;
      cin >> str;
      auto it = freq.find( str );
      if ( it == freq.end() ) cout << "0\n";
      else                    cout << it->second << '\n';
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <map>
#include <set>

int main() {
	int n {};
	std::map<std::string, unsigned> freq;

	for (std::cin >> n; n--; ) {
		unsigned m {};
		std::set<std::string> S;

		for (std::cin >> m; m--; ) {
			std::string word;

			freq[word] += S.insert((std::cin >> word, word)).second;
		}
	}

	unsigned q {};

	for (std::cin >> q; q--; ) {
		std::string str;

		if (const auto it { freq.find((std::cin >> str, str)) }; it == freq.end())
			std::cout << "0\n";
		else
			std::cout << it->second << '\n';
	}
}



5
2
A AB
1
A
3
A AB ABC
5
A AB ABC ABCD ABCDE
4
A AB ABC ABCD
3
AB
4
C
0
ABCDE
1

Last edited on
Topic archived. No new replies allowed.