SGU 505 Prefixes and suffixes
题解 现将字符串排序; 那么某前缀在字符串中出现肯定是连续的;写几个案例就知道了;这是记录每个字符在以前缀排名的rank ; 然后将字符串反序; 再排序;依照前缀,可以知道相同名字的后缀也会出现在一段排序好的连续的字符串里面;这样得到前缀的区间为 [a,b], [c,d]; 只要统计每个字符是否在 a 到 b 之间; 同时满足在 c 到 d 之间; 获取某个前缀的第一个匹配段字符串 和 最后一个字符串也就是 [a,b] 使用了字典树搞; 然后 再用线段树保留最大值和最小值;竟然没有超时, 啊,,哈哈;
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>
using namespace std; struct date{
int sta,end;
date *next[];
}*root,*rot,tree[]; int total;
date *creat_node(){
for( int i = ; i < ; i++ )
tree[total].next[i] = NULL;
tree[total].sta = -;
tree[total].end = -;
return &tree[total++];
}
void inint( ){
total = ;
root = creat_node();
rot = creat_node();
}
void insert( string &word,int i,int tab )
{
date *p; int len = word.length();
if( tab )p = root;
else p = rot;
for( int j = ; j < len; j++ )
{
int num = word[j] - 'a';
if( p->next[num] == NULL )
{
p->next[num] = creat_node();
p->next[num]->sta = i;
}
p->next[num]->end = i;
p = p->next[num];
}
}
int sta,en;
void work( string &word,int tab )
{
date *p; int len = word.length();
if( tab ) p = root;
else p = rot;
for( int j = ; j < len; j++ )
{
int num = word[j] - 'a';
if( p->next[num] == NULL ){
sta = -; en = -; return;
}
p = p->next[num]; sta = p->sta; en = p->end;
}
}
struct DAte{
string sss; int pos;
bool operator <(const DAte &a )const{
return sss < a.sss;
}
}arr[];
struct Date{
int lt,rt,Max,Min,num;
}node[];
void build( int lt,int rt,int t ){
node[t].lt = lt; node[t].rt = rt;
if( lt == rt ){ node[t].num = ; node[t].Max = node[t].Min = arr[lt].pos; return; }
int mid = ( lt+rt )>>;
build( lt,mid,t<< ); build( mid+,rt,t<<| );
node[t].Max = max( node[t<<].Max,node[t<<|].Max );
node[t].Min = min( node[t<<].Min,node[t<<|].Min );
node[t].num = node[t<<].num+node[t<<|].num;
}
int query( int lt,int rt,int a,int b,int t )
{
if( node[t].Min > b || node[t].Max < a )return ;
int mid = ( node[t].lt+node[t].rt )>>;
if( node[t].lt == lt && node[t].rt == rt )
{
if( node[t].Max <= b && node[t].Min >= a )return node[t].num;
return query( lt,mid,a,b,t<< ) + query( mid+,rt,a,b,t<<| );
}
if( node[t<<].rt >= rt )return query( lt,rt,a,b,t<< );
else if( node[t<<|].lt <= lt )return query( lt,rt,a,b,t<<| );
else return query(lt,mid,a,b,t<<)+query(mid+,rt,a,b,t<<|);
}
int main( )
{
int N,M; string str;
while( scanf("%d",&N) != EOF )
{
for( int i = ; i < N; i++ )cin>>arr[i].sss;
sort(arr,arr+N); inint();
for( int i = ; i < N; i++ ) arr[i].pos = i;
for( int i = ; i < N; i++ ) insert(arr[i].sss,i, );
for( int i = ; i < N; i++ ) reverse(arr[i].sss.begin(),arr[i].sss.end());
sort(arr,arr+N); build(,N-,);
for( int i = ; i < N; i++ ) insert(arr[i].sss,i,);
scanf("%d",&M);
for( int i = ; i <= M; i++ ){
cin>>str; work(str,); int a = sta,c = en;
cin>>str; reverse(str.begin(),str.end());work(str,); int b = sta,d = en;
if( a == - || b == - )cout<<<<endl;
else cout<<query( b,d,a,c, )<<endl;
}
}
return ;
}
/*
14
abasbssbs
sfasffsd
adfsas
fdsssf
safas
fsadf
fases
sdesas
aesdf
sefss
aseesaes
fdsasesa
seasea
sedfsas
11
a a
ab ac
fa a
fa s
ac ca
fd sa
fd as
se sa
fs fd
fd fs
ab sb
*/
SGU 505 Prefixes and suffixes的更多相关文章
- codeforces432D Prefixes and Suffixes(kmp+dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud D. Prefixes and Suffixes You have a strin ...
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes(后缀数组orKMP)
D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces 432 D. Prefixes and Suffixes
用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...
- Codeforces 1092C Prefixes and Suffixes(思维)
题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...
- CodeForces Round #527 (Div3) C. Prefixes and Suffixes
http://codeforces.com/contest/1092/problem/C Ivan wants to play a game with you. He picked some stri ...
- Codeforces 432D Prefixes and Suffixes kmp
手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...
- Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes
D. Prefixes and Suffixes You have a string s = s ...
- CF432D Prefixes and Suffixes
CF432D Prefixes and Suffixes 题意 给你一个长度为n的长字符串,"完美子串"既是它的前缀也是它的后缀,求"完美子串"的个数且统计这些 ...
随机推荐
- 记录一次代码错误,elastic search的INDEX需要使用小写字母
Caused by: org.elasticsearch.hadoop.EsHadoopIllegalArgumentException: Cannot determine write shards ...
- LVS基本介绍
一.LVS概念 LVS是Linux Virtual Server的简称,也就是Linux虚拟服务器.这是一个由章文嵩博士发起的一个开源项目,它的官方网站是http://www.linuxvirtual ...
- jquery扩展鼠标mousewheel事件
最近项目中要有个鼠标在图片上滚动实现图片放大和缩小的得到功能,隐约的记得好像记得有个 mousewheel 事件,可以实现需求,于是乎,开始在网上查找这一方法,但是,出乎意料的结果,各浏览器对此方法有 ...
- poj2996
/*排序函数要写对,优先级:K,Q,R,B,N,P 白色的:如果优先级一样,那么按照行数大的优先,如果行数一样,那么列数小的优先 黑色的:如果优先级一样,那么按照行数小的优先,如果行数一样,那么列数小 ...
- 自动回复之实现随机回复与常用Mapper XML标签
[常用Mapper XML标签] 1.基本的:select.insert.update 等 2.可读性.方便拼SQL:where.set.trim 3.减少重复:sql 4.逻辑控制:if.choos ...
- 使用js写的确认提交表单确认框
js脚本 <script language="Javascript"> function validator() { if(confirm("确认要执行此操作 ...
- 第七届蓝桥杯C-B-10-最大比例/gcd变形
最大比例 X星球的某个大奖赛设了M级奖励.每个级别的奖金是一个正整数.并且,相邻的两个级别间的比例是个固定值.也就是说:所有级别的奖金数构成了一个等比数列.比如:16,24,36,54其等比值为:3/ ...
- HDU 4531 bfs/康拓展开
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4531 吉哥系列故事——乾坤大挪移 Time Limit: 2000/1000 MS (Java/Othe ...
- Flexible实现H5页面的rem布局适配
1.https://www.w3cplus.com/mobile/lib-flexible-for-html5-layout.html (使用Flexible实现手淘H5页面的终端适配) 2.http ...
- 【第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛-J】 强迫症的序列
小A是一个中度强迫症患者,每次做数组有关的题目都异常难受,他十分希望数组的每一个元素都一样大,这样子看起来才是最棒的,所以他决定通过一些操作把这个变成一个看起来不难受的数组,但他又想不要和之前的那个数 ...