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

    1. Sun JDK 1.6 GC(Garbage Collector) http://bluedavy.com2010-05-13 V0.2 2010-05-19 V0.52010-06-01 V0 ...

  2. JS中的正则应用

    如果还未掌握正则基础知识可先看另一篇:正则笔记-忘记就来看 创建方法: 直接量语法:/pattern/attributes 创建 RegExp 对象的语法:new RegExp(pattern, at ...

  3. HDU4628

    /*状态转移f[i]=min(f[i],f[j]+f[i^j]); 就是j状态+i^j状态=i状态,f[i]记录的是从i删除1要的最小步数*/ #include<string.h> #in ...

  4. Java遍历List集合的三种方法

    Java遍历List集合的三种方法 List<String> list = new ArrayList<String>(); list.add("aaa") ...

  5. iOS7中彻底隐藏status bar

    用Xcode5开发新游戏,发现在iOS7中按照以前的方法隐藏status bar失效了. 想要彻底隐藏status bar,需要在info.plist中添加新行“View controller-bas ...

  6. 详细解读ARM寄存器之CPSR【转】

    本文转载自:https://blog.csdn.net/david_luyang/article/details/6276533 详细解读ARM寄存器之CPSR 整理人:卢阳 QQ:820927872 ...

  7. HDU 1159 Common Subsequence 动态规划

    2017-08-06 15:41:04 writer:pprp 刚开始学dp,集训的讲的很难,但是还是得自己看,从简单到难,慢慢来(如果哪里有错误欢迎各位大佬指正) 题意如下: 给两个字符串,找到其中 ...

  8. Android开发中的logcat工具使用

    http://os.51cto.com/art/200905/126051.htm 用adb直接查看log:    adb logcat 清除之前的log: adb logcat -c 加过滤查看lo ...

  9. linux ioctl()函数

    我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围.写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑. ...

  10. Ubuntu 16 下面的文件比较工具 Meld

    安装 sudo apt-get install meld 使用 很好用,很方便.支持文件比较,文件夹比较.