P1203 [USACO1.1]坏掉的项链Broken Necklace

P1203

题解一(暴力枚举):
因为n只有350,所以可以考虑暴力枚举每一个断点,时间复杂度n2

需要注意最后的答案不能大于n!

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PIL pair<int, ll>
#define endl '\n'
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define IOS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define debug(x) cerr << #x << " = " << x << endl
#define debug2(x, y) cerr << #x << " = " << x << " and " << #y << " = " << y << endl
#define Pi acos(-1)
using namespace std;

const int N = 5e5 + 100;
string a;

int f(int x)
{
    int res = 0;
    string p = "";
    for(int i = x; i < a.size(); ++ i) p += a[i];
    for(int i = 0; i < x; ++ i) p += a[i];
    //模拟剪短过程,断口的端口就是p的左右端点

	//向后找最多多少个
    int k = 0;
    while(p[k] == 'w') k ++, res ++;
    for(int i = k; i < p.size(); ++ i)
    {
        if(p[i] != p[k] && p[i] != 'w') break;
        res ++;
    }

	//向前找最多多少个
    k = p.size() - 1;
    while(p[k] == 'w') k --, res ++;
    for(int i = k; i >= 0; -- i)
    {
        if(p[i] != p[k] && p[i] != 'w') break;
        res ++;
    }

    return res;
}

void solve()
{
    int n;
    int MAX = -1;
    cin >> n;
    cin >> a;
    for(int i = 0; i < a.size(); ++ i) //枚举每一个断点
        MAX = max(MAX, f(i));
    cout << min(n, MAX) << endl;
}

int main() 
{
    IOS;
    solve();
    return 0;
}

题解二(dp):

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值