poj3415

本文针对CommonSubstrings问题,提出了一种使用后缀数组结合单调栈优化的解决方案。该方案能够在给定两个字符串A和B及整数K的情况下,高效计算出所有长度大于等于K的公共子串的数量之和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

Common Substrings
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 10932 Accepted: 3622
Description

A substring of a string T is defined as:

T(i, k)=TiTi+1…Ti+k-1, 1≤i≤i+k-1≤|T|.
Given two strings A, B and one integer K, we define S, a set of triples (i, j, k):

S = {(i, j, k) | k≥K, A(i, k)=B(j, k)}.
You are to give the value of |S| for specific A, B and K.

Input

The input file contains several blocks of data. For each block, the first line contains one integer K, followed by two lines containing strings A and B, respectively. The input file is ended by K=0.

1 ≤ |A|, |B| ≤ 105
1 ≤ K ≤ min{|A|, |B|}
Characters of A and B are all Latin letters.

Output

For each case, output an integer |S|.

Sample Input

2
aababaa
abaabaa
1
xx
xx
0
Sample Output

22
5

题解

简化问题,发现是问从i,j两个位置开始的长度大于等于k的公共字符串的个数和,那么考虑用后缀数组做
但是如果在处理出height之后直接做的话时间复杂度是n^2的,所以考虑用一个单调栈优化
st1,st2是两个单调栈,存储的是与当前字符串的公共前缀的长度,cc1,cc2表示对应的个数
那么我们在给排序后的后缀一次求值的时候顺便维护一下这两个单调栈的信息,就可以快速的求值了
这题其实比较好想,但是发现之前学sa时候的模板有一个问题,x,y数组范围应该开到字符串的两倍才对啊。。。

贴代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#define fo(i,a,b) for(i=a;i<=b;i++)
#define ll long long
using namespace std;

const int ma1=200005,maxn=100005;

int sa[ma1],rank[ma1],height[ma1],x[ma1*2],y[ma1*2],buc[ma1],st1[ma1],st2[ma1],cc1[ma1],cc2[ma1];
char s1[maxn],s[ma1];
int i,j,k,l,n,m,len,len1,len2,p,t1,t2;
int now1,now2,top1,top2;
ll ans,zong1,zong2;
void gesa(){
    m=96+26;
    memset(x,0,sizeof(x));
    fo(i,0,len-1) x[i]=s[i];
    fo(i,1,m) buc[i]=0;
    fo(i,0,len-1) buc[x[i]]++;
    fo(i,1,m) buc[i]+=buc[i-1];
    fo(i,0,len-1) sa[buc[x[i]]--]=i;
    for(k=1;k<len;k=k*2){
        p=0;
        fo(i,len-k,len-1) y[++p]=i;
        fo(i,1,len) if (sa[i]>=k) y[++p]=sa[i]-k;
        fo(i,1,m) buc[i]=0;
        fo(i,0,len-1) buc[x[i]]++;
        fo(i,1,m) buc[i]+=buc[i-1];
        for(i=len;i>=1;i--) sa[buc[x[y[i]]]--]=y[i];
        fo(i,0,len*2) y[i]=x[i];
        x[sa[1]]=1;
        fo(i,2,len){
            if (y[sa[i]]==y[sa[i-1]] && y[sa[i]+k]==y[sa[i-1]+k]) x[sa[i]]=x[sa[i-1]]; else
            x[sa[i]]=x[sa[i-1]]+1;
        }
        m=x[sa[len]];
        if (m==len) break;
    }
    fo(i,1,len) rank[sa[i]]=i;
}

void gehei(){
    k=0;
    fo(i,0,len-1){
        if (rank[i]==1){
            height[rank[i]]=0;
            continue;
        }
        if (k) k--;
        t1=i+k; t2=sa[rank[i]-1]+k;
        while (t1<len && t2<len){
            if (s[t1]=='#' || s[t2]=='#') break;
            if (s[t1]==s[t2]) k++; else break;
            t1++; t2++;
        }
        height[rank[i]]=k;
        if (s[i]=='#') height[i]=0;
    }
}
void geans(){
    ans=0;
    memset(cc1,0,sizeof(cc1));
    memset(cc2,0,sizeof(cc2));
    zong1=zong2=0;
    top1=top2=1;
    int x;
    k=n;
    fo(i,1,len)
    if (s[sa[i]]!='#')
    {
        x=height[i]-k+1;
        while (top2>1 && st2[top2-1]>=x) {
            zong2-=(st2[top2]-st2[top2-1])*cc2[top2];
            cc2[top2-1]+=cc2[top2]; cc2[top2--]=0;
        }

        if (st2[top2]>x && x>0){
            zong2-=(st2[top2]-x)*(cc2[top2]);
            st2[top2]=x;
        }
        while (top1>1 && st1[top1-1]>=x){
            zong1-=(st1[top1]-st1[top1-1])*cc1[top1];
            cc1[top1-1]+=cc1[top1]; cc1[top1--]=0;
        }
        if (st1[top1]>x && x>0){
            zong1-=(st1[top1]-x)*(cc1[top1]);
            st1[top1]=x;
        }
        if (x<=0) continue;
        if (sa[i-1]<len1){
            if (st1[top1]<x) st1[++top1]=x;
            cc1[top1]+=1;
            zong1+=x;
        } else{
            if (st2[top2]<x) st2[++top2]=x;
            cc2[top2]+=1;
            zong2+=x;
        }
        if (sa[i]<len1) ans+=zong2; else ans+=zong1;
    }
    printf("%lld\n",ans);
}
int main(){
    freopen("3415.in","r",stdin);
    freopen("3415.out","w",stdout);
    n=1;
    while (n){
        scanf("%d",&n);
        if (!n) break;
        scanf("%s",&s1);
        len1=strlen(s1);
        fo(i,0,len1-1) s[i]=s1[i];
        scanf("%s",&s1);
        len2=strlen(s1);
        s[len1]='#';
        fo(i,len1+1,len1+len2) s[i]=s1[i-len1-1];
        len=len1+len2+1;
        gesa();
        gehei();
        geans();
    }
    return 0;
}
资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化和响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”和“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)主要负责样式设计。它首先清除了一些默认的边距和填充,对 html 和 body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值