[Reading]

Title Description

English Teacher left N papers reading comprehension work, but there are a lot of words of each English essay needs to look up, in order to save time, now to be a statistic, calculate some new words which have appeared in several essay.

Input and output formats

Input formats:

 

The first behavior integer N, the number of short articles, wherein Each essay containing only spaces and lowercase letters.

Press down the N rows, each row describes an essay. Beginning of each line is an integer L, it represents the essay by the L-word. Next is L words, separated by a space between words.

Then as an integer M, expressed several times asked to do. Followed by M lines, each line represents a new word to the statistics.

 

Output formats:

 

For each line of output words, which appeared in the statistics which several short, the press from small to large number of output short, there should be no duplicate number, spaced apart (note that a number of the first number with a space between the front and after the last number we should have no spaces). If the word has not appeared, the output of a blank line.

 

Sample input and output

Input Sample # 1:

3
9 you are a good boy ha ha o yeah
13 o my god you like bleach naruto one piece and so do i
11 but i do not think you will get all the points
5
you
i
o
all
naruto

Output Sample # 1:

1 2 3
2 3
1 2
3
2

Problem-solving ideas

  The first is a dictionary tree, but we set a bool array to determine whether a word appears in an article too, then ......... last point on the WA (I was also Mongolia) Originally, we bool array defines the bitset can be replaced, so that the time complexity can / 32, very practical, strong Amway.

answer

 1 #include<bits/stdc++.h>
 2 #define N 300001
 3 using namespace std;
 4 int n,m,l; 
 5 char x[10001];
 6 struct Trie{
 7     int sz;
 8     int ch[N][26];
 9     bitset<1001>val[N];//bitset 
10     Trie(){
11         sz=1;
12         memset(ch[0],0,sizeof(CH [ 0 ]));
 13 is          Memset (Val, 0 , the sizeof (Val));
 14      } // Initialization 
15      int IDX ( char C) // character-to-digital 
16      {
 . 17          return the C- ' A ' ;
 18 is      }
 . 19      void iNSERT ( String S, int NUM) // insert 
20 is      {
 21 is          int U = 0 ;
 22 is          for ( int= I 0 ; I <s.size (); I ++ )
 23 is          {
 24              int C = IDX (S [I]);
 25              IF ! ( CH [U] [C])
 26 is              {
 27                  Memset (CH [SZ], 0 , the sizeof (CH [SZ]));
 28                  CH [U] [C] = SZ;
 29                  SZ ++ ;
 30              }
 31 is              U = CH [U] [C]; // downwardly conveniently 
32          }
 33 is          Val [U] [num] = 1 ; // appear in the first num articles over 
34     }
 35      void Find ( String S) // Find 
36      {
 37 [          int U = 0 ;
 38 is          for ( int I = 0 ; I <s.size (); I ++ )
 39          {
 40              int C = IDX (S [I]) ;
 41 is              IF (! CH [U] [C]) // not found 
42 is              {
 43 is                  COUT << endl;
 44 is                  return ;
 45              }
 46 is              U = CH [U] [C];
 47         }
 48          for ( int I = . 1 ; I <= n-; I ++) // look articles which appeared in 
49          {
 50              IF (Val [U] [I] == . 1 ) COUT << << I "  " ;
 51 is          }
 52 is          COUT << endl;
 53 is      }
 54 is  } Tree;
 55  int main ()
 56 is  {
 57 is      Scanf ( " % D " , & n-);
 58      for ( int I = . 1 ; I <= n-; I ++ )
 59     {
60         cin>>l;
61         for(int j=1;j<=l;j++)
62         {
63             scanf("%s",x);
64             tree.insert(x,i);
65         }
66     }
67     scanf("%d",&m);
68     for(int i=1;i<=m;i++)
69     {
70         scanf("%s",x);
71         tree.find(x);
72     }
73     return 0;
74 }

 

Guess you like

Origin www.cnblogs.com/hualian/p/11206745.html