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
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;
}