题解   现将字符串排序; 那么某前缀在字符串中出现肯定是连续的;写几个案例就知道了;这是记录每个字符在以前缀排名的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的更多相关文章

  1. codeforces432D Prefixes and Suffixes(kmp+dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Prefixes and Suffixes You have a strin ...

  2. Codeforces 432D Prefixes and Suffixes(KMP+dp)

    题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...

  3. 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 ...

  4. Codeforces 432 D. Prefixes and Suffixes

    用扩展KMP做简单省力..... D. Prefixes and Suffixes time limit per test 1 second memory limit per test 256 meg ...

  5. Codeforces 1092C Prefixes and Suffixes(思维)

    题目链接:Prefixes and Suffixes 题意:给定未知字符串长度n,给出2n-2个字符串,其中n-1个为未知字符串的前缀(n-1个字符串长度从1到n-1),另外n-1个为未知字符串的后缀 ...

  6. 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 ...

  7. Codeforces 432D Prefixes and Suffixes kmp

    手动转田神的大作:http://blog.csdn.net/tc_to_top/article/details/38793973 D. Prefixes and Suffixes time limit ...

  8. Codeforces Round #246 (Div. 2) D. Prefixes and Suffixes

                                                        D. Prefixes and Suffixes You have a string s = s ...

  9. CF432D Prefixes and Suffixes

    CF432D Prefixes and Suffixes 题意 给你一个长度为n的长字符串,"完美子串"既是它的前缀也是它的后缀,求"完美子串"的个数且统计这些 ...

随机推荐

  1. 对Java 注解的一些理解

    转载自https://blog.csdn.net/javazejian/article/details/71860633 引入 注解最简单的使用方式 Java注解与普通修饰符(public\stati ...

  2. Xshell 或者 Xftp 突然连不上阿里云

    今天突然发现使用xshell 远程连接不上阿里云,废了好大的劲,才明白的差不多.只要不出意外有以下几种情况:1.安全组中有没有你需要连接的端口,2.防火墙是否关闭,3.端口是否开放权限,4.是否安装了 ...

  3. ubuntu linux 1604 编译安装tesseract-ocr 4.0

    主要参考官方的编译,梳理一下整个流程 Linux The build instructions for Linux also apply to other UNIX like operating sy ...

  4. java中线程状态-死亡

    线程死亡: 线程会以如下3种方式结束,结束后就处于死亡状态. 1.run()或call()方法执行完成,线程正常结束. 2.线程抛出一个未捕获的Exception或Error 3.直接调用该线程的st ...

  5. html checkbox多选框语法与结构

    <input name="Fruit" type="checkbox" value="" /> 用法用例 <foreach ...

  6. File类之常用方法

    package IoDemo; import java.io.File; import java.io.IOException; /** * @Title:FileTest * @Descriptio ...

  7. Gamma函数相关matlab代码

    1.Gamma函数: Gamma函数matlab代码: x=:0.5:5syms t y=)*exp(-t),,inf) y=double(y) plot(x,y,) 图像如下: 2.lgΓ(x)函数 ...

  8. LeetCode 之 TwoSum

    题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...

  9. Pandas统计函数

    统计方法有助于理解和分析数据的行为.现在我们将学习一些统计函数,可以将这些函数应用到Pandas的对象上. pct_change()函数 系列,DatFrames和Panel都有pct_change( ...

  10. 从TensorFlow 到 Caffe2:盘点深度学习框架

    机器之心报道 本文首先介绍GitHub中最受欢迎的开源深度学习框架排名,然后再对其进行系统地对比 下图总结了在GitHub中最受欢迎的开源深度学习框架排名,该排名是基于各大框架在GitHub里的收藏数 ...