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的长字符串,"完美子串"既是它的前缀也是它的后缀,求"完美子串"的个数且统计这些 ...
随机推荐
- 使用 libevent 和 libev 提高网络应用性能(IBM)
http://www.ibm.com/developerworks/cn/aix/library/au-libev/
- akka框架地址
http://doc.akka.io/docs/akka/2.2.3/AkkaJava.pdf
- 【Github教程】史上最全github使用方法:github入门到精通(转自eoeandroid.com)
本文来源:http://www.eoeandroid.com/thread-274556-1-1.html 另附经典教程网址 :http://wuyuans.com/2012/05/github-si ...
- Sybase:游标用法以及嵌套用法
Sybase:游标用法以及嵌套用法 游标示例一: --Sybase游标示例一: create PROCEDURE DBA.p_proc_test() ON EXCEPTION RESUME begin ...
- 《Python学习手册》(四)
List and Dictionary 列表: 任意对象的有序集合 通过偏移读取 可变.异构.任意嵌套 常用方法: L.append(object) L.extend(iterable) L.inse ...
- camera frame work v3 note
1 android_atomic_write(level, &gLogLevel); 原子写操作. 2 构造函数和onFirstRef onFirstRef 会在构造函数运行后执行,这个是在m ...
- JSONObject使用方法
转载:http://blog.csdn.net/dongzhouzhou/article/details/8664569 1.JSONObject介绍 JSONObject-lib包是一个beans, ...
- Mysql数据库导出sql脚本
1. 运行环境Centos mysqldump -h localhost -u root -p etv > ./etv.sql etv 是要导出的数据库名 > 设置导出的路径和文件名
- 记录使用Buildbot遇到的坑
Buildbot Tips Buildbot也是个大坑..我并不熟悉python,偏偏文档又少.这几天使用buildbot出了不少坑.有的解决了,有的绕过去,这里都把它们一一记下来. Force Bu ...
- DFS - 深度搜索 - 基于邻接列表表示法
2017-07-25 15:38:00 writer:pprp 在前一篇图基于邻接列表表示法的代码加了一小部分,加了一个DFS函数,visited[N]数组 参考书目:张新华的<算法竞赛宝典&g ...