zoukankan      html  css  js  c++  java
  • HDU 3363 Ice-sugar Gourd (贪心)

    题意:给你一个串,串中有H跟T两种字符,然后切任意刀,使得能把H跟T各自分为原来的一半。

    析:由于只有两个字母,那么只要可以分成两份,那么一定有一段是连续的。

    代码如下:

    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <string>
    #include <algorithm>
    #include <vector>
    #include <map>
    using namespace std ;
    typedef long long LL;
    const int maxn = 1e5 + 5;
    const int INF = 0x3f3f3f3f;
    char s[maxn];
    
    int main(){
        int n, x;
        while(scanf("%d", &n) == 1 && n){
            scanf("%s", s);
            int h = 0, t = 0;
            for(int i = 0; i < n; ++i){
                if(s[i] == 'H') ++h;
                else ++t;
            }
            if((t&1) || (h&1)){
                printf("-1
    ");
                continue;
            }
            t /= 2;
            h /= 2;
    
            int ss = 0, e = 0;
            int cnth = 0, cntt = 0;
            while(ss < n){
                bool ok = false;
                while(e < n && cnth + cntt < n/2){
                    s[e] == 'H' ? ++cnth : ++cntt;
                    ++e;
                    if(cnth == h && cntt == t){
                        ok = true;
                        if(ss == 0)  printf("1
    ");
                        else  printf("2
    ");
                        if(ss == 0)  printf("%d
    ", n/2);
                        else  printf("%d %d
    ", ss, ss+n/2);
                    }
    
                }
                if(ok) break;
                s[ss] == 'H' ? --cnth : --cntt;
                ++ss;
            }
        }
        return 0;
    }
    
  • 相关阅读:
    探索Javascript 异步编程
    前端性能调优
    怎样选择前端框架
    前端地图截屏方案
    不一样的山顶角
    前后端分离场景下的另类登录认证方案
    React Diff 算法
    纯css实现Magicline Navigation(下划线动画导航菜单)
    Tinymce group plugin
    自适应process组件
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5708667.html
Copyright © 2011-2022 走看看