首页 > 其他分享 >[?] 字典树

[?] 字典树

时间:2023-07-17 20:22:31浏览次数:18  
标签:std 3e6 int && sc 字典

模板在此

点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int N=3e6+110;
int read(){
	int x=0,f=1;char c=getchar();
	while(c>'9' || c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0' && c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return x*f;
}
int T,n,q,t[N][65],rt,newpos,cnt[N];char s[N];
int tn(char sc){
	if(sc>='A' && sc<='Z')return sc-'A'+1;
	if(sc>='a' && sc<='z')return sc-'a'+1+26;
	if(sc>='0' && sc<='9')return sc-'0'+1+26+26;
	return 0;
}
void Clear(int u){
	for(int i=1;i<=26+26+10;i++){
		if(t[u][i]){
			Clear(t[u][i]);
			t[u][i]=0;
		}
	}
	return;
}
void solve(){
	for(int i=1;i<=newpos;i++)cnt[i]=0;Clear(rt);
	rt=newpos=1;
	n=read(),q=read();
	for(int i=1;i<=n;i++){
		scanf("%s",s);int len=strlen(s),np=rt;
		for(int j=0;j<len;j++){
			int w=tn(s[j]);
			if(t[np][w])np=t[np][w];
			else np=t[np][w]=++newpos;
			cnt[np]++;
		}
	}
	for(int i=1;i<=q;i++){
		scanf("%s",s);int len=strlen(s),np=rt,flag=1;
		for(int j=0;j<len;j++){
			int w=tn(s[j]);
			if(t[np][w])np=t[np][w];
			else{flag=0;break;}
		}
		if(flag)printf("%d\n",cnt[np]);
		else printf("0\n");
	}
	return;
}
int main(){
	T=read();
	while(T--)solve();
	return 0;
}

标签:std,3e6,int,&&,sc,字典
From: https://www.cnblogs.com/FJOI/p/17561094.html

相关文章

  • 字符串,列表的内置方法(增加、修改、删除) 、可变类型与不可变类型 、字典 ,元组,集合的
    字符串的内置方法(较多,重要)old_code='KeViN'print('这是返回给用户的验证码:%s'%old_code)new_code=input('请输入你的验证码:').strip()print(new_code)#对验证码作一个判断,现在对验证码作不区分带小写#ifold_code.upper()==new_code.upper():ifold_code.......
  • 字典,元组,元组内置方法、相关面试题 、 集合的内置方法 、字符编码 、文件操作 、函数
    字典的内置方法1.定义方式 d={'usernamne':"kevin"}#定义空字典d={}info=dict(username='kevin',age=18)#{'username':'kevin','age':18} print(info) #dic={#'name':�......
  • Oracle数据字典(各种视图、表)
    数据字典是存放整个数据库实例重要信息的一组表,这些数据字典大部分都是SYS用户所有。数据字典的构成Oracle数据字典名称由前缀和后缀组成,使用下画线“_”连接。其代表的含义如下。USER_:记录用户的对象信息。ALL_:记录用户的对象信息及被授权访问的对象信息。DBA_:包含数据......
  • Python【3】有序字典 OrderdDict
    有序字典可以按字典中元素的插入顺序来输出。参考https://www.cnblogs.com/lowmanisbusy/p/10257360.htmlimportcollectionsmy_order_dict=collections.OrderedDict()my_order_dict["name"]="lowman"my_order_dict["age"]=45my_order_dict["money&......
  • JavaScript:将对象数组映射到字典
    JavaScript:将对象数组映射到字典#javascript#打字稿#数据在JavaScript/TypeScript中将对象数组转换为字典的最简单方法:letdata=[{id:1,country:'Germany',population:83623528},{id:2,country:'Austria',population:8975552},{id:3,country......
  • template里面,显示字典dict的数据
    以下的例子是不可以的,obj.field  obj只能是modelinstance,字典对象不可以pythondict_data={'key1':0,'key2':1,}template{{dict_data.key1}} 对策:编写[email protected]_value(value,key):if(keyinvalue......
  • python怎么把字典写到文件中
    Python如何把字典写入文件中在Python中,我们可以使用多种方法将字典写入文件中。本文将介绍两种常用的方法:使用json模块和使用pickle模块。方法一:使用json模块json模块提供了将Python对象序列化为JSON格式的方法。字典是一种常见的Python对象,因此我们可以使用json.dump()或json.d......
  • 学生签到字典
    ('2017-03-1311:50:09',271,131),('2017-03-1416:52:19',273,131),('2017-03-1311:50:19',271,126),importref_path='d:/1/qian.txt'dict1={}withopen(f_path)asfiles:forfileinfiles:time_qian......
  • Python-{}字典dict.py
     1#!/usr/bin/python 2#coding=UTF-8 3 4''' 5Python字典 6 7字典(dictionary)是除列表以外python之中最灵活的内置数据结构类型。列表是有序的对象集合,字典是无序的对象集合。 8 9两者之间的区别在于:字典当中的元素是通过键来存取的,而不是通过偏移......
  • 若依获取字典表数据
    this.getDicts("xsq").then(response=>{constxsqs=response.data;for(leti=0;i<xsqs.length;i++){constxqpjTable={};xqpjTable.index=i;xqpjTable.id=xsqs[i].dictValue;xqpjTable.cpbm=xsqs[i].dictLabel;xq......