zoukankan      html  css  js  c++  java
  • Codeforces アンバランス / Unbalanced

    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+1sb(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

     

    needed
    

    Sample Output 1

     

    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

     

    atcoder
    

    Sample Output 2

     

    -1 -1
    

    The string atcoder contains no unbalanced substring.

    说实话,水题一道,但没想出来还真就凉凉,开始用前缀和,毕竟才100*n*26,但是死活最后一个过不了,于是换了个方法。

    一想最坏的情况就是隔一个一个相同,不然如果各两个就已经不可能是一半相同的了,于是只需要相隔一个相同或者相邻相同就直接可以输出了。

    #include<iostream>
    using namespace std;
    int main()
    {
    	char arr[100000];
    	scanf("%s",&arr);
    	int n=strlen(arr);
    	int p=0,a,b;
    	for(int i=0;i<n-2;i++)
    	{
    		if(arr[i]==arr[i+1])
    		{
    			p=1;
    			a=i;
    			b=i+1;
    			break;
    		}
    		else if(arr[i]==arr[i+2])
    		{
    			p=1;
    			a=i;
    			b=i+2;
    			break;
    		}
    	}
    	if(arr[n-2]==arr[n-1])
    	{
    		p=1;
    		a=n-2;
    		b=n-1;
    	}
    	if(p)
    	{
    		cout<<a+1<<' '<<b+1;
    	}
    	else
    	{
    		cout<<"-1 -1";
    	}
    }
    

     

  • 相关阅读:
    一个完善的ActiveX Web控件教程
    用ATL开发和部署ActiveX网页控件
    非IE内核浏览器支持activex插件
    OCX控件在IE中无法侦测到键盘消息( MFC ActiveX Control in IE Doesn't Detect Keystrokes)
    CImageList使用简要说明
    VC,一条会被鼠标移动的直线
    Java NIO学习笔记之基本概念
    netty源码分析
    如何绕过验证码方式总结
    解决Eclipse 启动后总是Building WorkSpace(sleeping)
  • 原文地址:https://www.cnblogs.com/mozheaishang/p/10017125.html
Copyright © 2011-2022 走看看