最小表示法 KMP 扩展KMP Manacher 模板

本文深入探讨了字符串处理中的关键算法,包括最小表示法结合KMP算法解决字符串旋转问题,Manacher算法寻找最长回文子串,以及扩展KMP算法进行高效匹配。通过具体实例展示了这些算法的应用场景和实现细节。

最小表示法+KMP

String Problem

 HDU - 3374 

Give you a string with length N, you can generate N strings by left shifts. For example let consider the string “SKYLONG”, we can generate seven strings: 
String Rank 
SKYLONG 1 
KYLONGS 2 
YLONGSK 3 
LONGSKY 4 
ONGSKYL 5 
NGSKYLO 6 
GSKYLON 7 
and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once. 
  Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also. 

Input

  Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.

Output

Output four integers separated by one space, lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), the string’s times in the N generated strings, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.

Sample Input

abcder
aaaaaa
ababab

Sample Output

1 1 6 1
1 6 1 6
1 3 2 3
#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
#include<set>
#include<queue>
using namespace std;
char ss[1000005];
int net[1000005];
int getminindex()
{
    int len=strlen(ss);
    int i=0,j=1,k=0;
    while(i<len&&j<len&&k<len){
        if(ss[(i+k)%len]==ss[(j+k)%len])
            k++;
        else if(ss[(i+k)%len]<ss[(j+k)%len]){
            j=j+k+1;
            k=0;
        }
        else{
            i=i+k+1;
            k=0;
        }
        if(i==j)
            j++;
    }
    return i<j?i:j;
}
int getmaxindex()
{
    int len=strlen(ss);
    int i=0,j=1,k=0;
    while(i<len&&j<len&&k<len){
        if(ss[(i+k)%len]==ss[(j+k)%len])
            k++;
        else if(ss[(i+k)%len]>ss[(j+k)%len]){
            j=j+k+1;
            k=0;
        }
        else{
            i=i+k+1;
            k=0;
        }
        if(i==j)
            j++;
    }
    return i<j?i:j;
}
void getnet()
{
    int len=strlen(ss);
    for(int i=1;i<len;i++){
        int j=net[i-1];
        while(ss[j]!=ss[i]&&j>0)
            j=net[j-1];
        if(ss[j]==ss[i])
            net[i]=j+1;
        else
            net[i]=0;
    }
}
int main()
{
    while(scanf("%s",ss)!=-1){
        getnet();
        int len=strlen(ss);
        int mincycle=len-net[len-1];
        int m=1;
        if(len%mincycle==0)
            m=len/mincycle;
        printf("%d %d %d %d\n",getminindex()+1,m,getmaxindex()+1,m);
    }
    return 0;
}

Manacher

T - Palindrome

 POJ - 3974 

Andy the smart computer science student was attending an algorithms class when the professor asked the students a simple question, "Can you propose an efficient algorithm to find the length of the largest palindrome in a string?" 

A string is said to be a palindrome if it reads the same both forwards and backwards, for example "madam" is a palindrome while "acm" is not. 

The students recognized that this is a classical problem but couldn't come up with a solution better than iterating over all substrings and checking whether they are palindrome or not, obviously this algorithm is not efficient at all, after a while Andy raised his hand and said "Okay, I've a better algorithm" and before he starts to explain his idea he stopped for a moment and then said "Well, I've an even better algorithm!". 

If you think you know Andy's final solution then prove it! Given a string of at most 1000000 characters find and print the length of the largest palindrome inside this string.

Input

Your program will be tested on at most 30 test cases, each test case is given as a string of at most 1000000 lowercase characters on a line by itself. The input is terminated by a line that starts with the string "END" (quotes for clarity). 

Output

For each test case in the input print the test case number and the length of the largest palindrome. 

Sample Input

abcbabcbabcba
abacacbaaaab
END

Sample Output

Case 1: 13
Case 2: 6
#include<iostream>
#include<stack>
#include<cstring>
#include<cstdio>
#include<set>
#include<queue>
#include<map>
#include<iostream>
#include<string>
using namespace std;
const int num=1000010;
int r[num*2];
char ss[num],ssa[num*2];
int get_r()
{
    ssa[0]='#';
    int len=1;
    int lenss=strlen(ss);
    for(int i=0; i<lenss; i++)
    {
        ssa[len++]=ss[i];
        ssa[len++]='#';
    }
    ssa[len]='\0';
    int bound=0,cent=0,maxr=0;
    for(int i=0; i<len; i++)
    {
        if(i<bound)
            r[i]=min(r[cent*2-i],bound-i);
        else
            r[i]=0;
        while(i-r[i]-1>=0&&i+r[i]+1<len&&ssa[i-r[i]-1]==ssa[i+r[i]+1])
            r[i]++;
        if(bound<i+r[i])
        {
            cent=i;
            bound=i+r[i];
        }
        maxr=max(maxr,r[i]);
    }
    return maxr;
}
int main()
{
    int cnt=1;
    while(scanf("%s",ss)!=-1)
    {
        if(ss[0]=='E')
            break;
        memset(r,0,sizeof(r));
        printf("Case %d: %d\n",cnt++,get_r());
    }
    return 0;
}

扩展KMP

const int maxn=100010;
int net[maxn],ex[maxn];
void getnet(char* s2)
{

    int i=0,j,po,len=strlen(s2);
    net[0]=len;
    while(s2[i]==s2[i+1]&&i+1<len)
        i++;
    net[1]=i;
    po=1;
    for(i=2; i<len; i++){
        if(net[i-po]+i<net[po]+po)
            net[i]=net[i-po];
        else{
            j=net[po]+po-i;
            if(j<0)
                j=0;
            while(i+j<len&&s2[j]==s2[j+i])
                j++;
            net[i]=j;
            po=i;
        }
    }
}
void getex(char* s1,char* s2)
{
    int i=0,j,po,len1=strlen(s1),len2=strlen(s2);
    getnet(s2);
    while(s1[i]==s2[i]&&i<len1&&i<len2)
        i++;
    ex[0]=i;
    po=0;
    for(i=1; i<len1; i++)
    {
        if(net[i-po]+i<ex[po]+po)
            ex[i]=net[i-po];
        else{
            j=ex[po]+po-i;
            if(j<0)
                j=0;
            while(i+j<len1&&j<len2&&s1[j+i]==s2[j])
                j++;
            ex[i]=j;
            po=i;
        }
    }
}

 

内容概要:本文介绍了一种基于蒙特卡洛模拟和拉格朗日优化方法的电动汽车充电站有序充电调度策略,重点针对分时电价机制下的分散式优化问题。通过Matlab代码实现,构建了考虑用户充电需求、电网负荷平衡及电价波动的数学模【电动汽车充电站有序充电调度的分散式优化】基于蒙特卡诺和拉格朗日的电动汽车优化调度(分时电价调度)(Matlab代码实现)型,采用拉格朗日乘子法处理约束条件,结合蒙特卡洛方法模拟大量电动汽车的随机充电行为,实现对充电功率和时间的优化分配,旨在降低用户充电成本、平抑电网峰谷差并提升充电站运营效率。该方法体现了智能优化算法在电力系统调度中的实际应用价值。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事新能源汽车、智能电网相关领域的工程技术人员。; 使用场景及目标:①研究电动汽车有序充电调度策略的设计与仿真;②学习蒙特卡洛模拟与拉格朗日优化在能源系统中的联合应用;③掌握基于分时电价的需求响应优化建模方法;④为微电网、充电站运营管理提供技术支持和决策参考。; 阅读建议:建议读者结合Matlab代码深入理解算法实现细节,重点关注目标函数构建、约束条件处理及优化求解过程,可尝试调整参数设置以观察不同场景下的调度效果,进一步拓展至多目标优化或多类型负荷协调调度的研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值