hihoCoder 1260 String Problem I
来源:互联网 发布:js indexof 数组 编辑:程序博客网 时间:2024/04/29 16:23
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
- 样例输入
3 3touristpetrrngtoosimplergptr
样例输出
0
1
1
解题思路:
用字典树做的,将第1个字符串集建树,对第2个字符串,去树上匹配,要在树上跳过1个节点的才是满足条件的,于是就递归枚举跳过的节点就行了。
代码:
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <string>#include <vector>#include <map>#include <set>using namespace std;const int maxn=100000+100;char s[maxn];const int Max=27;struct node{ int sign; int next[Max];} a[maxn];set<int> se;int cur=0;void insert(char *s){ int len,ans; int p=0; len=strlen(s); for(int i=0; i<len; i++) { ans=s[i]-'a'; if(a[p].next[ans]!=0) { p=a[p].next[ans]; } else { a[p].next[ans]=++cur; a[cur].sign=0; p=a[p].next[ans]; } } a[p].sign++;}//int ans;int len;void find(int sign,int p,int x){ if(x==len&&sign&&a[p].sign) { // ans+=a[p].sign; se.insert(p); //cout<<ans<<endl; return; } if(sign==0&&x<=len) { for(int i=0; i<26; i++) { if(a[p].next[i]>0) { int tp=a[p].next[i]; find(1,tp,x); } } } if(x<len) { int ne=s[x]-'a'; if(a[p].next[ne]>0) { int tp=a[p].next[ne]; find(sign,tp,x+1); } }}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=0; i<maxn; i++) { cur=0; a[i].sign=0; memset(a[i].next,0,sizeof(a[i].next)); } for(int i=0; i<n; i++) { scanf("%s",s); insert(s); } for(int i=0; i<m; i++) { se.clear(); scanf("%s",s); len=strlen(s); //ans=0; find(0,0,0); printf("%d\n",se.size()); } } return 0;}
描述
我们有一个字符串集合S,其中有N个两两不同的字符串。
还有M个询问,每个询问给出一个字符串w,求有多少S中的字符串可以由w添加恰好一个字母得到。
字母可以添加在包括开头结尾在内的任意位置,比如在"abc"中添加"x",就可能得到"xabc", "axbc", "abxc", "abcx".这4种串。
输入
第一行两个数N和M,表示集合S中字符串的数量和询问的数量。
接下来N行,其中第i行给出S中第i个字符串。
接下来M行,其中第i行给出第i个询问串。
所有字符串只由小写字母构成。
数据范围:
N,M<=10000。
S中字符串长度和<=100000。
所有询问中字符串长度和<=100000。
输出
对每个询问输出一个数表示答案。
0 0
- hihoCoder 1260 String Problem I
- hihoCoder 1260 String Problem I
- hihoCoder挑战赛17 A String Problem I 题库 1260
- hihoCoder 1260 String Problem I 【字典树 + DFS】
- hihocoder 1260 String Problem I (Trie树 好题)
- Hihocoder 挑战赛17 String Problem I (字符串HASH)
- hihoCoder 1261 String Problem II
- hihoCoder挑战赛17 B String Problem II hihoCoder 1261
- hihocoder 1261 String Problem II (Trie树)
- Hihocoder 1430 Problem I. A Boring Problem (二项式定理
- hihoCoder 1261 String Problem II 【字典树 + DFS】
- hiho挑战赛17 A String Problem I
- hihocoder Beautiful String
- hihoCoder 1061 Beautiful String
- hihocoder #1061 : Beautiful String
- Problem I
- Problem I
- Problem I
- 数据结构_1:线性表: C++
- 36项修炼:为自己的人生负责
- jQuery学习-宏观
- ios基础动画
- Vim插件自定义-mark.vim修改搜索匹配热键
- hihoCoder 1260 String Problem I
- 高斯模糊的算法
- C语言时间函数及结构化数据
- 学习牛人的精神吧
- MyEclipse10在读取XML文件中的数据找不到指定路径
- java的赋值
- DBA解题集:学会数数
- Java基础第一课
- android 4.2 数据库