Repository

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5048    Accepted Submission(s): 1739

Problem Description
When
you go shopping, you can search in repository for avalible merchandises
by the computers and internet. First you give the search system a name
about something, then the system responds with the results. Now you are
given a lot merchandise names in repository and some queries, and
required to simulate the process.
Input
There
is only one case. First there is an integer P
(1<=P<=10000)representing the number of the merchanidse names in
the repository. The next P lines each contain a string (it's length
isn't beyond 20,and all the letters are lowercase).Then there is an
integer Q(1<=Q<=100000) representing the number of the queries.
The next Q lines each contains a string(the same limitation as foregoing
descriptions) as the searching condition.
Output
For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
Sample Input
20
ae
af
ag
ah
ai
aj
ak
al
ads
add
ade
adf
adg
adh
adi
adj
adk
adl
aes
5
b
a
d
ad
s
Sample Output
0
20
11
11
2
我好气呀 g++无限爆内存 换c++过了 无奈下顺便写了个静态的
题意就是有n个字符串,m个询问  问字符串在n个字符串中出现过多少次
比如 abcd 中有a,b,c,d,abcd,bcd,cd,bc,abc...
我们可以将abcd拆成abcd bcd cd d 分别建树,每个字符计算下数字
但abab会导致重复,所以我们设置一下标记,看代码
动态建树,g++爆内存  c++交
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
struct tire{
int id,num;
tire *next[];
};
tire *root;
void insert(char *s,int k){
tire *p,*q;
p=root;
int len=strlen(s);
for(int i=;i<len;i++){
//cout<<3<<endl;
int t=s[i]-'a';
if(p->next[t]==NULL){
q=(tire *)malloc(sizeof(tire));
for(int j=;j<;j++)q->next[j]=NULL;
q->num=;
q->id=-;
p->next[t]=q;
}
p=p->next[t];
if(p->id!=k){
p->id=k;
p->num++;
}
}
}
int find(char *s){
tire *p=root;
int len=strlen(s);
for(int i=;i<len;i++){
int t=s[i]-'a';
if(p->next[t]==NULL)return ;
else
p=p->next[t];
}
return p->num;
}
int main(){
int m,n;
char str[];
root=(tire *)malloc(sizeof(tire));
for(int i=;i<;i++)root->next[i]=NULL;
root->id=-;
root->num=;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",str);
int len=strlen(str);
for(int j=;j<len;j++){
//cout<<1<<endl;
insert(str+j,i);//cout<<2<<endl;
}
}
scanf("%d",&m);
while(m--){
scanf("%s",str);
cout<<find(str)<<endl;
}
}

接下来是一个静态的字典树(节省内存) c++ g++都可以过

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cstdlib>
#include<string>
#define maxnode 500000
#define sigma_size 30
#define eps 0.000000001
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
int ch[maxnode][sigma_size];
int val[maxnode];
int flag[maxnode];
int sz;
void init(){
memset(ch[],,sizeof(ch[]));
sz=;
}
int idx(char c){
return c-'a';
}
void insert(char *s,int k){
int u=;
int len=strlen(s);
for(int i=;i<len;i++){
int c=idx(s[i]);
if(ch[u][c]==){
memset(ch[sz],,sizeof(ch[sz]));
val[sz]=;
ch[u][c]=sz++;
}
u=ch[u][c];
if(flag[u]!=k){
val[u]++;
flag[u]=k;
}
}
}
int find(char *s){
int u=;
int len=strlen(s);
for(int i=;i<len;i++){
int c=idx(s[i]);
if(ch[u][c]==)return ;
u=ch[u][c];
}
return val[u];
}
int main(){
int m,n;
init();
memset(flag,-,sizeof(flag));
//for(int i=0;i<10;i++)cout<<flag[i]<<" ";
char str[];
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",str);
int len=strlen(str);
for(int j=;j<len;j++){
//cout<<1<<endl;
insert(str+j,i);//cout<<2<<endl;
}
}
scanf("%d",&m);
while(m--){
scanf("%s",str);
cout<<find(str)<<endl;
}
}
Source

最新文章

  1. [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)
  2. win7 64+python2.7.12安装numpy+scipy+matplotlib+scikit-learn
  3. Windows Store App 访问应用内部文件
  4. Linux(Ubuntu) 和 Python 常用命令
  5. 导入myeclipse项目出现的问题及解决方案
  6. 九度OJ 1349 数字在排序数组中出现的次数 -- 二分查找
  7. Java基础之Throwable,文件加载
  8. 网络编程基础+UDP的实现
  9. 网站开发进阶(四十四)input type=&quot;submit&quot; 和&quot;button&quot;的区别
  10. mysql 连接超慢
  11. Elinks介绍
  12. shogun docker image 中import shogun error
  13. os模块(操作系统)
  14. 【原创】分布式之redis的三大衍生数据结构
  15. prompt更改MySQL登陆后的提示符
  16. Java代码中获取Json的key值
  17. 以太坊 web3.js 文档翻译及说明
  18. 纯小白入手 vue3.0 CLI - 2.5 - 了解组件的三维
  19. Linux共享库 配置文件读取
  20. C 复制字符串

热门文章

  1. JS——模拟百度搜索
  2. 用sed替换含反斜(\)的字符串
  3. kickstart配置文件详解和system-config-kickstart (转载)
  4. CSS居中布局方案
  5. NGINX+PHP-FPM7 FastCGI sent in stderr: “Primary script unknown”
  6. C++入职学习篇--代码规范(持续更新)
  7. 学生成绩管理系统C(链表)语言
  8. Python - 模块(二)
  9. 一个电商项目的Web服务化改造6:单元测试4步走,构造数据、执行操作、断言、回滚
  10. 【[Offer收割]编程练习赛12 A】歌德巴赫猜想