当前位置:网站首页>[dictionary tree] [trie] p3879 [tjoi2010] reading comprehension
[dictionary tree] [trie] p3879 [tjoi2010] reading comprehension
2022-07-06 07:23:00 【Chasing the beacon】
P3879 [TJOI2010] reading comprehension
Dictionary tree writing
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
int nex[300007][26],n,cnt=0;
bitset<1001> b[500007];//bool b[500007][1001];
void insert(char* s,int rol){
int len=strlen(s+1);
int now=0;
FOR(i,1,len){
int p=s[i]-'a';
if(!nex[now][p])// If $Trie$ The tree is numbered without the prefix of this word
nex[now][p]=++cnt;// The number mentioned above
now=nex[now][p];// Then go deeper , Update the current location
}
b[now][rol]=1;// This word is in the x There's a line
}
void check(char* s){
int len=strlen(s+1);
int now=0,flag=1;
FOR(i,1,len){
int p=s[i]-'a';
if(!nex[now][p]){
// If in Trie There is no current character in the tree , You can do it directly break It fell off
flag=0;
break;
}
now=nex[now][p];// Otherwise, update the location
}
if(flag){
FOR(i,1,n)// On the surface of the question, it is said to output in dictionary order
if(b[now][i]) cout<<i<<" ";// In which sentences does the output appear
}
putchar('\n');
}
int main(){
cin>>n;
char s[25];
FOR(i,1,n){
int l;cin>>l;
FOR(j,1,l){
// Word by word insertion Trie In the tree
cin>>(s+1);
insert(s,i);
}
}
int m;cin>>m;
FOR(i,1,m){
cin>>(s+1);
check(s);
}
return 0;
}
STL: MAP + VECTOR How to write it
use STL You don't have to worry about how big the array is and how big the space is , But it will take more time
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn = 100001;
int n,m,num,cnt[maxn];
string s;
map<string,vector<int> >a;
int main(){
cin>>n;
FOR(i,1,n){
cin>>num;
FOR(j,1,num){
cin>>s;
a[s].push_back(i);// Every word is a vector, Save the number of sentences
}
}
cin>>m;
FOR(i,1,m){
cin>>s;
mem(cnt);//cnt Is to remove the heavy bucket . Every time you use it to output different queries, you need to clear .
int len=a[s].size();
FOR(j,0,len-1)
if(cnt[a[s][j]] == 0){
cout<<a[s][j]<<" ";
cnt[a[s][j]]++;// Remove weight with bucket
}
cout<<endl;
}
return 0;
}
STL: MAP + SET How to write it
use set It can automatically remove the weight , More convenient . meanwhile Time consuming is excellent , Second only to handwriting Trie
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
using namespace std;
map<string,set<int> > m;
int main(){
int n,p,l;
string s;
cin>>n;
FOR(i,1,n){
cin>>l;// The number of words
FOR(j,0,l-1){
cin>>s;// word
m[s].insert(i);
}
}
cin>>p;
while(p--){
cin>>s;
if(m.count(s)){
// If m There are elements in s
for(auto iter=m[s].begin();iter!=m[s].end();++iter)
cout<<*iter<<" ";
}
cout<<endl;
}
return 0;
}
边栏推荐
- 作者已死?AI正用藝術征服人類
- 烧录场景下的源代码防泄密方案分享
- The way to learn go (I) the basic introduction of go to the first HelloWorld
- The best way to learn SEO: search engine
- Jerry needs to modify the profile definition of GATT [chapter]
- Cookie技术&Session技术&ServletContext对象
- Week6 weekly report
- Fundamentals of C language 9: Functions
- OpenJudge NOI 2.1 1749:数字方格
- Bugku CTF daily question: do you want seeds? Blackmailed
猜你喜欢

Cookie技术&Session技术&ServletContext对象

杰理之BLE【篇】

呆错图床系统源码图片CDN加速与破解防盗链功能

Redis builds clusters

Internal and external troubles of "boring ape" bayc

word中如何删除某符号前面或后面所有的文字

How Navicat imports MySQL scripts
![When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]](/img/3e/3d5bff87995b4a9fac093a6d9f9473.png)
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]

1091: two or three things in childhood (multi instance test)
![[MySQL learning notes 32] mvcc](/img/0d/2df82b63d1eb3283a84e27f67c1523.png)
[MySQL learning notes 32] mvcc
随机推荐
OpenJudge NOI 2.1 1661:Bomb Game
Do you really think binary search is easy
After the hot update of uniapp, "mismatched versions may cause application exceptions" causes and Solutions
[window] when the Microsoft Store is deleted locally, how to reinstall it in three steps
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
Seriously recommend several machine learning official account
Wechat brain competition answer applet_ Support the flow main belt with the latest question bank file
Ble of Jerry [chapter]
微信公众号无限回调授权系统源码 全网首发
Raspberry pie 3B update VIM
How to configure GUI guide development environment
Win10 64 bit Mitsubishi PLC software appears oleaut32 DLL access denied
[MySQL learning notes 29] trigger
Go learning --- use reflection to judge whether the value is valid
Week6 weekly report
How are the open source Netease cloud music API projects implemented?
word设置目录
[MySQL learning notes 32] mvcc
1091: two or three things in childhood (multi instance test)
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes