HDU 2594 Simpsons’ Hidden Talents

本文介绍了一种使用KMP算法解决字符串匹配问题的方法,包括不同实现方式及其应用场景,如寻找两个字符串之间的最长公共前后缀。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594

 

Problem Description

Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had.
Marge: Yeah, what is it?
Homer: Take me for example. I want to find out if I have a talent in politics, OK?
Marge: OK.
Homer: So I take some politician’s name, say Clinton, and try to find the length of the longest prefix
in Clinton’s name that is a suffix in my name. That’s how close I am to being a politician like Clinton
Marge: Why on earth choose the longest prefix that is a suffix???
Homer: Well, our talents are deeply hidden within ourselves, Marge.
Marge: So how close are you?
Homer: 0!
Marge: I’m not surprised.
Homer: But you know, you must have some real math talent hidden deep in you.
Marge: How come?
Homer: Riemann and Marjorie gives 3!!!
Marge: Who the heck is Riemann?
Homer: Never mind.
Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.

 

 

Input

Input consists of two lines. The first line contains s1 and the second line contains s2. You may assume all letters are in lowercase.

 

 

Output

Output consists of a single line that contains the longest string that is a prefix of s1 and a suffix of s2, followed by the length of that prefix. If the longest such string is the empty string, then the output should be 0.
The lengths of s1 and s2 will be at most 50000.

 

 

Sample Input

 

clinton

homer

riemann

marjorie

 

 

Sample Output

 

0

rie 3

 

题目大意:

求两个串的第一个串的前缀和第二个串的后缀的最长匹配

 

写法一:在中间添加一个大写字母,将两个串连在一起,上Get_next(),保证后面的后缀不会匹配到前面去

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;

string s1,s2,mo;
int Next[N];

void Get_next()
{
    int len = mo.length();
    int i,j;
    j = Next[0] = -1;
    i=0;
    while(i<len)
    {
        while(j!=-1&&mo[i]!=mo[j])
            j = Next[j];
        Next[++i] = ++j;
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    while(cin>>s1>>s2)
    {
        mo = s1+'A'+s2;
        int len = mo.length();
        Get_next();
        int t = Next[len];
        if(t==-1||t==0)
            cout<<"0"<<endl;
        else
        {
            for(int i = 0;i<t;i++)
                cout<<mo[i];
            cout<<" "<<t<<endl;
        }
    }
    return 0;
}

写法二:直接把两个字符串连接起来,上Get_next(),但是有点问题就是,直接用next数组的话会把后缀延伸到第一串当中,或者把前缀延伸到第二串当中,这样就不符合题意了。所以要继续next下去,直到长度小等于短串的长度,就行了。

 

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;

string s1,s2,mo;
int Next[N];

void Get_next()
{
    int len = mo.length();
    int i,j;
    j = Next[0] = -1;
    i=0;
    while(i<len)
    {
        while(j!=-1&&mo[i]!=mo[j])
            j = Next[j];
        Next[++i] = ++j;
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    while(cin>>s1>>s2)
    {
        mo = s1+s2;
        int len1 = s1.length();
        int len2 = s2.length();
        int len = mo.length();
        Get_next();
        int t = Next[len];
        while(t>len1||t>len2)
            t = Next[t];
        if(t==-1||t==0)
            cout<<"0"<<endl;
        else
        {
            for(int i = 0;i<t;i++)
                cout<<mo[i];
            cout<<" "<<t<<endl;
        }
    }
    return 0;
}

 

写法三:将第一个串当成模式串,第二个串去匹配,一直匹配到最后求出来的j就是答案

代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e6+5;

string str,mo;
int Next[N];

void Get_next()
{
    int len = mo.length();
    int i,j;
    j = Next[0] = -1;
    i=0;
    while(i<len)
    {
        while(j!=-1&&mo[i]!=mo[j])
            j = Next[j];
        Next[++i] = ++j;
    }
}

void KMP()
{
    int i=0,j=0;
    int len = str.length();
    int molen = mo.length();
    while(i<len)
    {
        while(j!=-1&&mo[j]!=str[i])
            j = Next[j];
        ++i;
        ++j;
    }
    if(j==-1||j==0)
        cout<<"0"<<endl;
    else
    {
        for(int k=0;k<j;k++)
            cout<<mo[k];
        cout<<" "<<j<<endl;
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    while(cin>>mo>>str)
    {
        Get_next();
        KMP();
    }
    return 0;
}

写法四:扩展KMP

#include<cstdio>
#include<cmath>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>

using namespace std;

#define FOU(i,x,y) for(int i=x;i<=y;i++)
#define FOD(i,x,y) for(int i=x;i>=y;i--)
#define MEM(a,val) memset(a,val,sizeof(a))
#define PI acos(-1.0)

const double EXP = 1e-9;
typedef long long ll;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const ll MINF = 0x3f3f3f3f3f3f3f3f;
const double DINF = 0xffffffffffff;
const int mod = 1e9+7;
const int N = 1e6+5;

//假设s为文本串(长度为n),T为模式串(长度为m)
char s[N],T[N];
int n,m;
int Next[N];    //Next[i]代表T[i~m-1]与T[0~m-1]最长公共前缀
int ex[N];      //extend数组,代表s[i~n-1]与T[0~m-1]最长公共前缀
        //注意到,如果有一个位置extend[i]=m,则表示T在S中出现,而且是在位置i出现,这就是标准的KMP问题
void Get_Next(){
    Next[0] = m;
    int j=0;
    while(j+1<m&&T[j]==T[j+1])  //计算Next[1]
        j++;
    Next[1] = j;
    int po=1;    //设po为答案伸得最远的后缀的下标
    for(int i=2;i<m;i++){
        int p = Next[po] + po - 1; //已匹配最大位置
        int L = Next[i-po];        //
        if(i+L<p+1)
            Next[i] = L;
        else{
            j = max(0,p-i+1);
            while(i+j<m && T[i+j] == T[j])
                j++;
            Next[i] = j;
            po = i;
        }
    }
}

void EX_KMP(){
    Get_Next();
    int j=0;
    while(j<n&&j<m&&s[j]==T[j])
        j++;
    ex[0] = j;
    int po = 0;
    for(int i=1;i<n;i++){
        int p = ex[po]+po-1;
        int L = ex[i-po];
        if(i+L<p+1)
            ex[i] = L;
        else{
            j = max(0,p-i+1);
            while(i+j<n&&j<m&&s[i+j]==T[j])
                j++;
            ex[i] = j;
            po = i;
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    std::ios::sync_with_stdio(false);
    while(~scanf(" %s %s",&T,&s)){
        n = strlen(s);
        m = strlen(T);
        EX_KMP();
        int k = 0;
        int len = 0;
        for(int i=0;i<n;i++){
            if(i+ex[i] == n){
                len = ex[i];
                break;
            }
        }
        for(int i=0;i<len;i++){
            printf("%c",T[i]);
        }
        if(len!=0)
            printf(" ");
        printf("%d\n",len);
    }
    return 0;
}

 

多源动态最优潮流的分布鲁棒优化方法(IEEE118节点)(Matlab代码实现)内容概要:本文介绍了基于Matlab代码实现的多源动态最优潮流的分布鲁棒优化方法,适用于IEEE118节点电力系统。该方法结合两阶段鲁棒模型与确定性模型,旨在应对电力系统中多源输入(如可再生能源)的不确定性,提升系统运行的安全性与经济性。文中详细阐述了分布鲁棒优化的建模思路,包括不确定性集合的构建、目标函数的设计以及约束条件的处理,并通过Matlab编程实现算法求解,提供了完整的仿真流程与结果分析。此外,文档还列举了大量相关电力系统优化研究案例,涵盖微电网调度、电动汽车集群并网、需求响应、储能配置等多个方向,展示了其在实际工程中的广泛应用价值。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事能源系统优化工作的工程师。; 使用场景及目标:①用于研究高比例可再生能源接入背景下电力系统的动态最优潮流问题;②支撑科研工作中对分布鲁棒优化模型的复现与改进;③为电力系统调度、规划及运行决策提供理论支持与仿真工具。; 阅读建议:建议读者结合提供的Matlab代码与IEEE118节点系统参数进行实操演练,深入理解分布鲁棒优化的建模逻辑与求解过程,同时可参考文中提及的其他优化案例拓展研究思路。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值