新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)A-Red Rover

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


时间限制: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

题目大意:题目大意,给你一个字符串,让你用一个字母M代替字符串当中的某一段字串(可以代替多段,如题目中的例子),让你求出运用此代替形成的字符串长度加上代替的那段字符串长度的最小值。
不太会string的附上: string的操作详解
解题思路:所以我们暴力枚举一下所有情况的字串,然后再从头到尾进行匹配就可以了。
AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#define sa(a) scanf("%d", &a);
#define bug printf("--------");
using namespace std;
typedef long long LL;
const int INF = 1e9;
const int mod  = 1e9+7;

string str, ch, s;

int main()
{
    while(cin>>str) {
        int ans = str.size();
        for(int len = 2; len <= (str.size()+1)/2; len ++) { //长度我们枚举一半即可
            for(int j = 0; j+len-1 < str.size(); j ++) {
                s = str.substr(j, len); //字串
                int cnt = 0;
                for(int i = 0; i+len-1 < str.size(); i ++) { //匹配
                    ch = str.substr(i, len);
                    if(ch == s) {
                        i += len-1;
                        cnt ++;
                    }
                }
                int s = str.size();
                ans = min(ans, s - cnt*(len-1) +len);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/i_believe_cwj/article/details/80197564
今日推荐