Red Rover 简单字符串应用

链接: https://www.nowcoder.com/acm/contest/116/A
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制: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);
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值