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

题目链接

大概意思就是,找出一个字串,将其所有出现的地方替换成一个字母M后的长度len1,然后再加上这个字串的长度t_len得到一个新的长度len,求这个len最小等于多少。

当然,也可以不找这个串,那len就是原串的长度。

// Asimple
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<" = "<<a<<endl
using namespace std;
typedef long long ll;
const int maxn = 20000 + 5;
int T, n, sum, num, m, t, len, ans;
string str;

void input() {
    cin >> str;
    int t_len = 0;
    int len = str.length();
    int ans = len;
    map<string, int> mmp;
    for(int i=0; i<len; i++) {
        string ss = "";
        ss = ss + str[i];
        for(int j=i+1; j<len; j++) {
            ss = ss + str[j];
            int t_len = ss.length();
            int res = 0;
            for(int k=0; k<=len-t_len; k++) {
                string sss = str.substr(k, t_len);
                if( sss == ss ) {
                    res ++;
                    k += t_len - 1;
                }
            }
            ans = min(ans, len-res*t_len+t_len+res);
        }
    }
    
    cout << ans << endl;
}

 
int main() {
    input();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Asimple/p/8979205.html