Problem E Red Rover kmp

One of our older Mars Rovers has nearly completed its tour of duty and is awaiting instructions
for one last mission to explore the Martian surface. The survey team has picked a route and has
entrusted you with the job of transmitting the final set of instructions to the rover. This route
is simply a sequence of moves in the cardinal directions: North, South, East, and West. These
instructions can be sent using a string of corresponding characters: N, S, E, and W. However,
receiving the signal drains the rover’s power supply, which is already dangerously low. Fortunately,
the rover’s creators built in the ability for you to optionally define a single “macro" that can be used
if the route has a lot of repetition. More concretely, to send a message with a macro, two strings are
sent. The first is over the characters {N,S,E,W,M} and the second is over {N,S,E,W}. The first
string represents a sequence ofmovesand callstoa macro(M),while thesecond stringdetermines
whatthemacroexpandsoutto. Forexample:
WNMWMME
EEN
isanencodingof
WNEENWEENEENE
Noticethattheversionwithmacrosrequires only10 characters,whereastheoriginalrequires13.
Givenaroute,determinetheminimum numberof charactersneededtotransmitittotherover.
Input
InputconsistsofasinglelinecontainingastringmadeupofthelettersN, S, E,andWrepresenting
theroutetotransmittotherover. Themaximum lengthofthestringis100.
Input
Displaytheminimumnumberofcharactersneededtoencodetheroute.
Sample Input 1 Sample Output 1
WNEENWEENEENE 10
Sample Input 2 Sample Output 2

NSEW 4



直接枚举各个长度,然后暴力kmp


ac代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char a[105];
int next[105];
void getNext(char t[],int len)
{
    memset(next,-1,sizeof(next));
    int i = 0,k=-1;
    while(i<len)
    {
        if(k==-1 || t[i]==t[k])
        {
            i++,k++;
            next[i] = k;
        }
        else
            k = next[k];
    }
}
int kmp(char s1[],char s2[],int len2)
{
    int len1 = strlen(s1);
    int i=0,j=0,cnt=0;
    getNext(s2,len2);
    while(i<len1 && j<len2)
    {
        if(s1[i]==s2[j])
        {
            i++;
            j++;
            if(j==len2)
            {
                cnt++;
                j = 0;
            }
        }
        else if(j==0)
            i++;
        else
            j = next[j];
    }
    return cnt;
}
int main()
{
    gets(a);
    int len =strlen(a);
    int sum= len;
    for(int i=0;i<len;i++)
    {
        for(int j=i+1;j<len;j++)
        {
            int g=0;
            char tmp[105];
            for(int k=i;k<=j;k++)
            tmp[g++]=a[k];
            int tt=kmp(a,tmp,g);
            int t1=g;
            sum=min(sum,len-tt*t1+tt+t1);
        }
    }
    printf("%d\n",sum);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值