two strings
A1484. two strings(罗干)
给定两个字符串A和B,有五种操作,操作1为在A串开头添加一个字符,操作2为在A串结尾添加一个字符,操作3为在B串开头添加一个字符,操作4为在B串结尾添加一个字符,操作5为询问当前的B串在当前A串中出现的次数。保证字符均为小写字母,且A、B串初始非空。
【输入格式】
第一行第二行分别为初始的A串和B串;
第三行一个整数m,表示操作的次数;
接下来m行,每行表示一个操作,每行第一个数为一个在1-5之间的数字,若其值不为5,则在数字后会有一个小写字母。
【输出格式】
对于每个询问,每行输出一个整数,表示B串在A串中出现的次数。
【样例输入】
ababc
a
7
5
4 b
5
3 a
1 a
5
5
【样例输出】
2
2
1
1
【数据规模】
10%的数据中,最终A串和B串长度之和小于等于200,操作数小于等于10。
30%的数据中,最终A串和B串长度之和小于等于2000,操作数小于等于1000。
100%的数据中,最终A串和B串长度之和小于等于200000,操作数小于等于200000。
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+,inf=0x3f3f3f3f;
int n,m,k,t,d[maxn],tot;
void add(int x,int y){while(x<=tot)d[x]+=y,x+=x&(-x);}
int get(int x){int ret=;while(x)ret+=d[x],x-=x&(-x);return ret;}
struct query{int op,pos;}q[maxn];
struct node{int x,id,nt;}e[maxn];
char ch[maxn];
int cntA[maxn],cntB[maxn];
int sa[maxn],lev[maxn],height[maxn];
int A[maxn],B[maxn],tsa[maxn];
int mi[][maxn],p[maxn];
int ask(int l,int r)
{
int x=p[r-l+];
return min(mi[x][l],mi[x][r-(<<x)+]);
}
void solve(int n,int m)
{
for (int i = ; i < m; i ++) cntA[i] = ;
for (int i = ; i <= n; i ++) cntA[ch[i]] ++;
for (int i = ; i < m; i ++) cntA[i] += cntA[i - ];
for (int i = n; i; i --) sa[cntA[ch[i]] --] = i;
lev[sa[]] = ;
for (int i = ; i <= n; i ++)
{
lev[sa[i]] = lev[sa[i - ]];
if (ch[sa[i]] != ch[sa[i - ]]) lev[sa[i]] ++;
}
for (int l = ; lev[sa[n]] < n; l <<= )
{
memset(cntA,,sizeof(cntA[])*(n+));
memset(cntB,,sizeof(cntB[])*(n+));
for (int i = ; i <= n; i ++)
{
cntA[A[i] = lev[i]] ++;
cntB[B[i] = (i + l <= n) ? lev[i + l] : ] ++;
}
for (int i = ; i <= n; i ++) cntB[i] += cntB[i - ];
for (int i = n; i; i --) tsa[cntB[B[i]] --] = i;
for (int i = ; i <= n; i ++) cntA[i] += cntA[i - ];
for (int i = n; i; i --) sa[cntA[A[tsa[i]]] --] = tsa[i];
lev[sa[]] = ;
for (int i = ; i <= n; i ++)
{
lev[sa[i]] = lev[sa[i - ]];
if (A[sa[i]] != A[sa[i - ]] || B[sa[i]] != B[sa[i - ]]) lev[sa[i]] ++;
}
}
for (int i = , j = ; i <= n; i ++)
{
if (j) j --;
while (ch[i + j] == ch[sa[lev[i] - ] + j]) j ++;
height[lev[i]] = j;
}
}
int main() {
int i,j;
//freopen("in.txt","r",stdin);
tot=;
int ha=-,ta=-,hb=-,tb=-;
scanf("%s",ch+);
for(i=;ch[i];i++)
{
e[tot].x=ch[i]-'a';
if(!~ha)ha=ta=tot;
else e[ta].nt=tot,ta=tot;
tot++;
}
scanf("%s",ch+);
for(i=;ch[i];i++)
{
e[tot].x=ch[i]-'a';
if(!~hb)hb=tb=tot;
else e[tb].nt=tot,tb=tot;
tot++;
}
//cout<<ha<<" "<<ta<<" "<<hb<<" "<<tb<<endl;
scanf("%d",&m);
for(i=;i<=m;i++)
{
int op;
char str[];
scanf("%d",&op);
q[i].op=op;
if(op==)continue;
scanf("%s",str);
e[tot].x=str[]-'a';
e[tot].id=i;
if(op==)
{
e[tot].nt=ha;
ha=tot;
}
else if(op==)
{
e[ta].nt=tot;
ta=tot;
}
else if(op==)
{
e[tot].nt=hb;
hb=tot;
}
else if(op==)
{
e[tb].nt=tot;
tb=tot;
}
tot++;
}
tot=;
int sx,sy,ex,ey;
sx=sy=-;
for(i=ha;;i=e[i].nt)
{
ch[++tot]=e[i].x;
if(e[i].id)q[e[i].id].pos=tot;
else {if(!~sx)sx=tot;ex=tot;}
if(i==ta)break;
}
ch[++tot]=;
for(i=hb;;i=e[i].nt)
{
ch[++tot]=e[i].x;
if(e[i].id)q[e[i].id].pos=tot;
else {if(!~sy)sy=tot;ey=tot;}
if(i==tb)break;
}
solve(tot,);
for(i=;i<=tot;i++)p[i]=+p[i>>];
for(i=;<<i<=tot;i++)
{
for(j=;j+(<<i)<=tot;j++)
{
if(!i)mi[i][j]=height[j];
else mi[i][j]=min(mi[i-][j],mi[i-][j+(<<(i-))]);
}
}
//cout<<sx<<" "<<ex<<" "<<sy<<" "<<ey<<endl;
for(i=sx;i+ey-sy<=ex;i++)add(lev[i],);
for(i=;i<=m;i++)
{
int op=q[i].op;
if(op==)
{
sx--;
if(sx+ey-sy<=ex)add(lev[sx],);
}
else if(op==)
{
ex++;
if(sx+ey-sy<=ex)add(lev[ex-ey+sy],);
}
else if(op==)
{
sy--;
if(ex-ey+sy+>=sx)add(lev[ex-ey+sy+],-);
}
else if(op==)
{
ey++;
if(ex-ey+sy+>=sx)add(lev[ex-ey+sy+],-);
}
else
{
int pos=lev[sy],len=ey-sy+;
int l=,r=pos-,pl=pos,pr=pos;
while(l<=r)
{
int mid=l+r>>;
if(ask(mid+,pos)>=len)pl=mid,r=mid-;
else l=mid+;
}
l=pos+,r=tot;
while(l<=r)
{
int mid=l+r>>;
if(ask(pos+,mid)>=len)pr=mid,l=mid+;
else r=mid-;
}
printf("%d\n",get(pr)-get(pl-));
}
}
return ;
}
two strings的更多相关文章
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 使用strings查看二进制文件中的字符串
使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...
- LeetCode 205 Isomorphic Strings
Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...
随机推荐
- 应用交付、负载均衡(Load balancing)、高可用、F5
“应用交付”,实际上就是指应用交付网络(Application Delivery Networking,简称ADN),它利用相应的网络优化/加速设备,确保用户的业务应用能够快速.安全.可靠地交付给内部 ...
- C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]
原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using Sy ...
- 转】RDD与DataFrame的转换
原博文出自于: http://www.cnblogs.com/namhwik/p/5967910.html RDD与DataFrame转换1. 通过反射的方式来推断RDD元素中的元数据.因为RDD本身 ...
- Spring日期格式初始化
原文链接地址 一.初始化参数绑定[一种日期格式] 配置步骤: ①:在applicationcontext.xml中只需要配置一个包扫描器即可 1 2 <!-- 包扫描器 --> ...
- AJPFX关于java 知识点的集合
1 .对象的初始化 (1 )非静态对象的初始化 在创建对象时,对象所在类的所有数据成员会首先进行初始化. 基本类型:int 型,初始化为0. 如果为对象:这些对象会按顺序初始化. ※在所有类成员初始化 ...
- Oracle 表-初步涉水不深(第一天)
oracle 关系型数据库 注释:面对大型数据处理,市场占有率40%多(但是目前正往分布式转换) 故事:本来一台大型计算机才能处理的数据,美国科学家用100台家用电脑连接,成功处理了数据.. tabl ...
- MYSQL5.7 忘记ROOT密码/初始化ROOT密码
编辑my.cnf允许空密码登录 [root@7Core ~]# vi /etc/my.cnf #在[mysqld]下加入一行 skip-grant-tables=1 重新启动Mysql服务 [root ...
- 一个小方法解决RGBA不兼容IE8
原网页http://blog.csdn.net/leihope_/article/details/70158902 要在一个页面中设置一个半透明的白色div.这个貌似不是难题,只需要给这个div设置如 ...
- 学习笔记 第九章 使用CSS美化表格
第9章 使用CSS美化表格 学习重点 正确使用表格标签: 设置表格和单元格属性: 设计表格的CSS样式. 9.1 表格的基本结构 表格由行.列.单元格3部分组成,单元格时行与列交叉的部分. 在HTM ...
- MTK处理器手机 解锁Bootloader 教程
目前很多手机都需要解锁Bootloader之后才能进行刷机操作 本篇教程教你如何傻瓜式解锁Bootloader 首先需要在设置-关于手机 找到版本号(个别手机可能是内核版本号,甚至其他) 然后 快 ...