zyl第十一次总结,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

文章讲述了在洛谷在线编程平台上遇到的两道题目,涉及KMP算法(用于字符串匹配)的Next函数实现以及RadioTransmission无线传输问题,反映了学习者在解决实际编程问题时的困惑和求知过程。
摘要由CSDN通过智能技术生成

写了几道题:

感觉这周任务有些难

next数组的代码实在有一些难以理解(什么流程还是知道),不过还好代码就短短几行,可以记住

P3375 【模板】KMP - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <bits/stdc++.h>
using namespace std;
int slen,tlen;
int next1[1000010];
char s[1000005];
    char t[1000005];
void  Next(string t)
{
    int i=-1,j=0;
    next1[0]=-1;
    while(j<tlen)
    {
        if(i==-1||t[i]==t[j])
        {
            next1[++j]=++i;
        }
        else
        {
            i=next1[i];
        }
    }
}

void kmp(string s,string t)
{
    int i=0,j=0;
    slen=s.size();
    tlen=t.size();
    Next(t);
    while(i<slen)
    {
        if(j==-1||s[i]==t[j])
        {
            i++;
            j++;
        }
        else
        {
            j=next1[j];
        }
        if(j==tlen)
        {
            printf("%d\n",i-tlen+1);
            j=next1[j];
        }
    }
}

int main()
{

    scanf("%s",s);
    scanf("%s",t);
    kmp(s,t);
    for(int i=1;i<=tlen;i++)
    {
        printf("%d ",next1[i]);
    }
    return 0;
}

P4391 [BOI2009] Radio Transmission 无线传输 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

这题我看了很久都不知道结论怎么来的,只好记了

#include<bits/stdc++.h>
using namespace std;

int next1[1000005];
char s[1000005];
int l;
int len;
void funnext()
{
    int i=-1,j=0;
    next1[0]=-1;
    while(j<len)
    {
        if(i==-1||s[i]==s[j])
        {
            next1[++j]=++i;
        }
        else
        {
            i=next1[i];
        }
    }
}

int main()
{
    scanf("%d",&l);
    scanf("%s",s);
    len=strlen(s);
    funnext();
    printf("%d",l-next1[l]);
    return 0;
}

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值