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 ...
随机推荐
- (三)python函数式编程
一.高阶函数 高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 结论:函数本身也可以赋值给变量,即:变量可以指向函数 ...
- 转 SQL*PLUS中的替换变量(& &&)
今天oracle support提供一个脚本,大致如下: PROMPT ROUTING_SEQUENCE_ID = &ROUT_SEQ_ID PROMPT OPERATION_SEQUENCE ...
- 199 Binary Tree Right Side View 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,返回从顶部到底部看到的节点值.例如:给定以下二叉树, 1 <--- / \2 3 <--- \ ...
- Spring注解驱动开发之声明式事务
前言:现今SpringBoot.SpringCloud技术非常火热,作为Spring之上的框架,他们大量使用到了Spring的一些底层注解.原理,比如@Conditional.@Import.@Ena ...
- turn协议的工作原理
Allocate请求 客户端通过发送Allocate请求给STUN服务器,从而让STUN服务器为A用户开启一个relay端口. a) 客户端A向STUN Port发送Allocate请求(图中 ...
- webpack 报错:Module build failed: Unknown word (1:1)
解决方法:一是确保css配置里的"style-loader"必须在"css-loader"之前,二是将整个css配置注释掉,如下图:
- 把WebForm移植到.Net MVC中
最近写项目,想把以前项目中的几个功能页面移植过来(想偷懒一下),在网上查了很多的资料,多数资料都是直接在MVC中添加WebForm,和我的需求不同.在此非常感谢网友“Jason”给予的帮助,终于搞定了 ...
- spring3 上配置quartz 任务调度
maven配置: <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>qu ...
- EcliplseJPA2.1和glassfish3.1兼容问题
之前一个项目,持久层用eclipseJpa2.1实现,web服务器用的是glassfish3.1. 部署完成后测试的时候出现bug,反反复复折腾了n次,最终确认是版本兼容的问题. 或者用glassfi ...
- 洛谷 P2341 [HAOI2006]受欢迎的牛
题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...