zoukankan      html  css  js  c++  java
  • Atcoder Beginner contest 043 D

    D - アンバランス / Unbalanced


    Time limit : 2sec / Memory limit : 256MB

    Score : 400 points

    Problem Statement

    Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both voodoo and melee are unbalanced, while neither noon nor a is.

    You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.

    Constraints

    • 2≦|s|≦105
    • s consists of lowercase letters.

    Partial Score

    • 200 points will be awarded for passing the test set satisfying 2≦N≦100.

    Input

    The input is given from Standard Input in the following format:

    s
    

    Output

    If there exists no unbalanced substring of s, print -1 -1.

    If there exists an unbalanced substring of s, let one such substring be sasa+1…sb (1≦a<b≦|s|), and print a b. If there exists more than one such substring, any of them will be accepted.


    Sample Input 1

    Copy
    needed
    

    Sample Output 1

    Copy
    2 5
    

    The string s2s3s4s5 = eede is unbalanced. There are also other unbalanced substrings. For example, the output 2 6 will also be accepted.


    Sample Input 2

    Copy
    atcoder
    

    Sample Output 2

    Copy
    -1 -1
    

    The string atcoder contains no unbalanced substring.

     题意:给定一个序列,如果某个子序列中的有一个字母的数量大于子序列长度的一半则为平衡序列。

    题解:其实本题只要判断了相邻两个字母是否一样或者间隔一个的字母是否相同就能判断出是否有子序列满足平衡。在往后判断只是重复了上述的情况。

    备注:遇到有多种情况输出,一般能找到某个规律使得输出情况最简易。

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int flag=0;
        string s;
        cin>>s;
        for(int i=0;i<s.size()-1;i++)
        {
            if(s[i]==s[i+1])
            {
                cout<<i+1<<" "<<i+2<<endl;
                flag=1;
                break;
            }
            else if(i<s.size()-2&&s[i]==s[i+2])
            {
                cout<<i+1<<" "<<i+3<<endl;
                flag=1;
                break;
            }
        }
        if(!flag)
        cout<<-1<<" "<<-1<<endl;
        return 0;    
    } 
    漫天星辰,繁华时下。心中冷淡,一笑奈何。
  • 相关阅读:
    QQ列表展示
    拖动条SeekBar
    信息提示框、对话框
    键盘事件、触摸事件
    时间日期事件处理,长按事件
    图片切换示例
    索引值的应用
    this的应用
    自定义属性应用1
    自定义属性应用
  • 原文地址:https://www.cnblogs.com/Scalpel-cold/p/7258341.html
Copyright © 2011-2022 走看看