KMP POJ 2752 Seek the Name, Seek the Fame

本文探讨了如何通过优化算法解决给定字符串中可能的最长公共前后缀问题,详细介绍了暴力求解方法导致的超时问题,并提出了利用KMP算法优化的解决方案。通过分析next数组的含义,解释了其在寻找最长公共前后缀中的应用,并提供了一个高效的实现方式。

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

Seek the Name, Seek the Fame
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15562 Accepted: 7875
Description
The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:

Step1. Connect the father’s name and the mother’s name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).

Example: Father=’ala’, Mother=’la’, we have S = ‘ala’+’la’ = ‘alala’. Potential prefix-suffix strings of S are {‘a’, ‘ala’, ‘alala’}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Input
The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby’s name.
Sample Input
ababcababababcabab
aaaaa
Sample Output
2 4 9 18
1 2 3 4 5
Source
POJ Monthly–2006.01.22,Zeyuan Zhu

暴力求解,TLE:

#include<stdio.h>
#include<stdlib.h> 
#include<algorithm>
#include<iostream>
#define MAX 400005 
using namespace std;
char in[MAX];
int ans[MAX],len,num,flag; 
int main(){
    freopen("i.txt","r",stdin); 
    while(cin>>in){
        len=strlen(in);
        num=0;       
        for(int i=1;i<=len;i++){
            flag=1;
            for(int j=1;j<=i;j++){
                if(in[len-j]!=in[i-j]){
                    flag=0;
                    break;
                }
            }
            if(flag) ans[num++]=i; 
        } 
        for(int i=0;i<num;i++)  cout<<ans[i]<<" ";
        cout<<endl;
    } 
    return 0;
}

利用KMP的getnext()函数,得到优化的程序:

//3172K 469MS
#include<stdio.h>
#include<stdlib.h> 
#include<algorithm>
#include<iostream>
#define MAX 400005 
using namespace std;
char s[MAX];
int next[MAX],ans[MAX],len,num=0;
void get_next(char *s,int*next){
    memset(next,0,sizeof(next));
    next[0]=-1;
    len=strlen(s);
    int suf=0,pre=-1;
    while(suf!-len) {
        if(pre==-1||s[suf]==s[pre]) next[++suf]=++pre;
        else pre=next[pre];
    }
}
int main(){
    freopen("i.txt","r",stdin);
    while(~scanf("%s",s)){
        get_next(s,next);  
        ans[0]=len;
        while(next[len]>0){
          ans[++num]=next[len];
          len=next[len];
        }
        for(int i=num;i>0;i--) 
            printf("%d ",ans[i]);
        printf("%d\n",ans[0]);
    }
}

要理解这样做的正确性,重点是理解next数组的含义:next[i]表示模式串前i个字符最大
相同前缀和后缀(不包括字符串本身)的长度。
从next数组最后一个开始往前寻找每个相同的后缀和前缀。
next数组:
这里写图片描述
假设next[len]=k,表示模式串前k个组成的前缀和最后k个字符组成后缀的是相同的。
那么下一个相同的后缀和前缀,最后一个字符一定相同,因为后缀的最后一个字符一定不会变,假设字符串最后一个字符用红色表示,那么所有的前缀的最后一个都应该是红色的,如图所示。因此,可以通过len=next[len]=k来寻找下一个相同的前缀和后缀。

这里写图片描述


模式串的前k个和后k个相同,前k个的前m个和后m个相同,等价代换得到模式串的前m个和后m个相同,于是又得到了一个相同的前缀和后缀。


欢迎留言,积极讨论,一起进步!

一、综合实战—使用极轴追踪方式绘制信号灯 实战目标:利用对象捕捉追踪和极轴追踪功能创建信号灯图形 技术要点:结合两种追踪方式实现精确绘图,适用于工程制图中需要精确定位的场景 1. 切换至AutoCAD 操作步骤: 启动AutoCAD 2016软件 打开随书光盘中的素材文件 确认工作空间为"草图与注释"模式 2. 绘图设置 1)草图设置对话框 打开方式:通过"工具→绘图设置"菜单命令 功能定位:该对话框包含捕捉、追踪等核心绘图辅助功能设置 2)对象捕捉设置 关键配置: 启用对象捕捉(F3快捷键) 启用对象捕捉追踪(F11快捷键) 勾选端点、中心、圆心、象限点等常用捕捉模式 追踪原理:命令执行时悬停光标可显示追踪矢量,再次悬停可停止追踪 3)极轴追踪设置 参数设置: 启用极轴追踪功能 设置角度增量为45度 确认后退出对话框 3. 绘制信号灯 1)绘制圆形 执行命令:"绘图→圆→圆心、半径"命令 绘制过程: 使用对象捕捉追踪定位矩形中心作为圆心 输入半径值30并按Enter确认 通过象限点捕捉确保圆形位置准确 2)绘制直线 操作要点: 选择"绘图→直线"命令 捕捉矩形上边中点作为起点 捕捉圆的上象限点作为终点 按Enter结束当前直线命令 重复技巧: 按Enter可重复最近使用的直线命令 通过圆心捕捉和极轴追踪绘制放射状直线 最终形成完整的信号灯指示图案 3)完成绘制 验证要点: 检查所有直线是否准确连接圆心和象限点 确认极轴追踪的45度增量是否体现 保存绘图文件(快捷键Ctrl+S)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值