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的长字符串,"完美子串"既是它的前缀也是它的后缀,求"完美子串"的个数且统计这些 ...
随机推荐
- cdoj1329卿学姐与魔法
地址:http://acm.uestc.edu.cn/#/problem/show/1329 题目: 卿学姐与魔法 Time Limit: 1200/800MS (Java/Others) M ...
- Unity 自定义编辑窗体之ScriptableWizard
当我们在编辑界面要批量设置游戏资源的时候,就需要从UnityEditor里面继承,实现自己的窗口类. 所幸UNITY提供了最简单的一个自定义窗体类,我们直接往上扔public类型的属性就好,提供了确认 ...
- mapreduce 运行-指定各种运行参数
mapreduce指定参数 mapreduce在运行的时候可以指定各种参数,这样可以根据实际的应用场景做一下相关的调整 1.指定运行时cpu的个数 hadoop jar hadoop-core-0.1 ...
- netty3---传统IO,NIO,nettyIO
传统: NIO: nettyIO: 每个服务生负责一个区域,每个服务生是一个线程.
- html4与html5的区别
一.HTML5更加灵活,支持下列多种形式 1.标签名可以大写(不推荐) -<SpAN>这个HTML5也的认</SpAN> 2.属性双引号可选(推荐添加双引号) -<div ...
- Python MySQL数据库连接模块
1. MySQLdb只支持在Python 2版本使用MySQLdb是用于Python链接Mysql数据库的接口.a.pip安装 直接使用pip进行安装,在此之前需要安装一些系统依赖包. ● CentO ...
- 安装pycurl
环境:ubuntu 1604 安装 pycurl 遇到一些问题 简单记录 1.安装 pippython2:apt install python-pippython3: apt install pyth ...
- 《网络攻防》Web安全基础实践
20145224陈颢文 <网络攻防>Web安全基础实践 基础问题回答 SQL注入攻击原理,如何防御: 部分程序员在编写代码的时候,没有对用户输入数据的合法性进行判断,黑客利用这个bug在数 ...
- MySQL性能调优思路
1.MySQL性能调优思路 如果一台服务器出现长时间负载过高 /周期性负载过大,或偶尔卡住如何来处理? 是周期性的变化还是偶尔问题?是服务器整体性能的问题, 还是某单条语句的问题? 具体到单条语句, ...
- [POI2012] BEZ-Minimalist Security
一张n个点m条边的无向图,有点权有边权都是非负,且每条边的权值小于等于两个顶点的权值和,现在要将每个点减一个非负整数使得每条边权等于两个顶点的点权和,问最大修改代价和最小修改代价 思路神的一匹,完全想 ...