String Deletion

链接:https://vjudge.net/contest/400848#problem/D
注:a/2向上取整就是(a+1)/2
代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
char s[maxn];
int a[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        scanf("%s", s+1);
        int now = 1, tot = 0;
        for(int i = 2; i <= n; i++) {
            if(s[i] == s[i-1]) {
                now++;
            }
            else{
                a[++tot] = now;
                now = 1;
            }
        }
        a[++tot] = now;
        int ans = 0;
        for(int i = 1; i <= tot; i++) {
            if(a[i] > 1) {
                ans++;
            }
            else {
                bool flag = 0;
                for(int j = i + 1; j <= tot; j++) {
                    if(a[j] > 1) {
                        a[j]--;
                        ans++;
                        flag = 1;
                        break;
                    }
                }
                if(!flag) {
                    ans += (tot-i+2)/2;
                    break;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}
全部评论

相关推荐

对空六翼:你真幸运,碰见这么好的人,不像我,秋招的时候被室友骗进cx了
实习好累,可以辞职全力准...
点赞 评论 收藏
分享
2025-12-17 15:08
赤峰学院 Java
乐邦詹雨:学历厂,另外东子不同部门待遇进的难度天差地别,我同学零售一面1h30min有八股算法项目拷打,我就两轮技术面各30min就一面问了些技术问题,二面问了两个开放式技术问题,三面线下hr面就结束了
点赞 评论 收藏
分享
评论
3
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务