A1484. two strings(罗干)

时间限制:1.0s   内存限制:256.0MB
【问题描述】
  给定两个字符串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。
分析:对最终的A串和B串用分隔符拼接,构建后缀数组,模拟这m次操作;
   可以发现对于在A串或B串添加字符,最多会影响一个位置的可行性;
   在后缀数组中查找字符串可以使用二分找到连续区间,询问区间的合法性可以使用树状数组实现;
代码:
  

#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的更多相关文章

  1. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

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

  3. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  5. [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 ...

  6. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  7. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  9. 使用strings查看二进制文件中的字符串

    使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...

  10. LeetCode 205 Isomorphic Strings

    Problem: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if ...

随机推荐

  1. 转 SQLPLUS中SQL换行执行

    权声明:本文为博主原创文章,未经博主允许不得转载. 正常情况下,在SQLPLUS中输入命令时,可以换行,但不能有空格,否则不能执行,会直接返回到SQL>下.但通过命令设置可以实现语句换行时允许有 ...

  2. 移动端如何定义字体font-family

    移动端如何定义字体font-family 中文字体使用系统默认即可,英文用Helvetica /* 移动端定义字体的代码 */ body{font-family:Helvetica;} 参考<移 ...

  3. java_dom4j解析xml

    package forRQ; import java.io.File;import java.net.MalformedURLException;import java.util.Iterator;i ...

  4. POI导出时,将指定的列设置为下拉列表

    本示例设置第2列为下拉框(下拉框内容为:是/否),从第5行开始到5657行结束. 关键代码示例: ComboxList = new String[]{"是","否&quo ...

  5. Android 给按钮添加监听事件

    在安卓开发中,如果要给一个按钮添加监听事件的话,有以下三种实现方式 1.方式一 public class MainActivity extends ActionBarActivity { @Overr ...

  6. 短视频SDK用于旅游行业

    超级简单易用的短视频SDK来自RDSDK.COM.锐动天地为开发者提供短视频编辑.视频直播.特效.录屏.编解码.视频转换,等多种解决方案,涵盖PC.iOS.Android多平台.以市场为导向,不断打磨 ...

  7. nginx教程从入门到精通

    [转]nginx教程从入门到精通 nginx教程写了一段时间,无意中发现,nginx相关文章已经达到了近100篇了.觉得很有必要汇总到一起,它是我们运维生存时间的一片心血,他是学习nginx的同学必看 ...

  8. 对象输入输出流ObjectInputStream、ObjectOutputStream(对象序列化与反序列化)

    对象的输入输出流 : 主要的作用是用于写入对象信息与读取对象信息. 对象信息一旦写到文件上那么对象的信息就可以做到持久化了 对象的输出流: ObjectOutputStream 对象的输入流:  Ob ...

  9. Youtube-dl 简短使用总结

    默认下载bestvideo+bestaudio,并通过ffmpeg -c copy output.mp4 简单的封装进mp4格式,而不进行转码. 有时候bestaudio 是opus编码的,但是mp4 ...

  10. C/C++ 标准输入、输出

    一.分类 1.标准输入输出 键盘输入,显示器输出.2.文件输入输出 以外存为对象,即硬盘.光盘等.3.串输入输出 对内存中指定空间进行输入输出. 二.c语言中的输入输出 #include <st ...