链接:
https://www.nowcoder.com/acm/contest/116/A
来源:牛客网
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
输入描述:
Input consists of a single line containing a string made up of the letters N, S, E, and W representing the route to transmit to the rover. The maximum length of the string is 100.
输出描述:
Display the minimum number of characters needed to encode the route.
示例1
输入
WNEENWEENEENE
输出
10
长度为100最多的子串为100*99/2,直接暴力,这里利用到c++的substr函数,substr(i,j)表示从字符串第i个位置长度为j的子串,然后再对字符串来一遍暴力,如果找到一个子串是匹配的,那么次数+1,然后跳过这个子串去寻找下一个,注意,这里的字符个数最大一定是字符串的长度,不要设置INF,初始值应该是ans=a.length().
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int i,j,k,len,ans;
string a,b,c;
cin>>a;
len=a.length();
ans=len;
for(i=0;i<=len-1;i++)
{
for(j=i+1;j<=len-1;j++)
{
b=a.substr(i,j-i+1);
int sum=0;
for(k=0;k<=len-1;k++)
{
c=a.substr(k,j-i+1);
if(b==c)
{
sum++;
k+=j-i;
}
}
// cout<<b<<' '<<ans<<' '<<sum<<' '<<j-i+1<<endl;
ans=min(ans,j-i+1+sum+len-sum*(j-i+1));
}
}
printf("%d\n",ans);
}