Brace matching depth, interleaving string 01 (a type code)

Title Description

A valid sequences matching parenthesis following definitions:
1, empty string "" is a valid sequence matching brackets
2, if the "X" and "Y" are legitimate sequence matching brackets, "the XY" is a valid matching brackets sequence
3, if the "X" is a legitimate bracket matching sequence, then "(X)" is also a valid sequence matching brackets
4, each bracket legitimate sequences may be generated by the above rule.
For example: "", "()", "() ()", "((()))" are legitimate parentheses sequence
for a legitimate parenthesis sequence we have the following definition of its depth:
1, an empty string "" depth of 0
2, if the string "X" is the depth x, the string "Y" is the depth y, the character string "XY" depth of max (x, y) 3, if the "X" the depth x, then the depth of the character string "(X-)" x + 1 is
for example: "() () ()" is the depth of 1 "((()))" is the depth of 3. Now to give you a legitimate beef brackets sequence, you need to calculate its depth.

Enter a description:

Input comprises a bracket valid sequence s, s longitudinal length (2 ≤ length ≤ 50), the sequence contains only '(' and ")".

Output Description:

It outputs a positive integer, i.e., the depth of this sequence.
Example 1

Entry

copy
(())

Export

copy
2
#include <the iostream> 
#include <the cmath> 
#include <algorithm> 
#include < SET > 
#include <cstdio> 
#include < String > 
#include <CString>
 / * @author: bk 
* Family: 
* Time: 
* / 
/ / I'm like a child playing at the beach,
 // time to time to pick up more than the usual smooth stones or more beautiful shells rejoice,
 // and demonstrated in front of me is not entirely proven sea truth 
using  namespace STD; 

int main () 
{ 

    String STR; 
    getline (CIN, STR); 

    int _max = 0 , now_max = 0;
     For ( int J = 0 ; J <str.size (); J ++ ) 
    { 
        IF (STR [J] == ' ( ' ) 
        { 
            now_max ++ ; 
            _max = (max _> now_max? _Max: now_max); 
        } 
        the else 
        { // calculation parentheses following the depth 
        now_max-- ;
         // write preceded now_max = 0; a set of tests can not pass
         // forget a case where "(((() (()))))" of right after many brackets may be nested left parenthesis, then recount even less 

        } 

    } 

    COUT << _ max << endl; 



    //<< COUT "the Hello World!" << endl; 
    return  0 ; 
} 
/ * 
Use Case: 
(((((((() (() ((())))))))))) 
corresponding to the output must be : 
11 
your output is: 
8 
* /

Title Description

If a 01 character string of any two adjacent positions are not the same, we call this string of 01 staggered 01 strings. For example: "1", "10101", "0101010" strings 01 are staggered.
Xiao Yi now has a string of 01 s, Xiao Yi wanted to find one of the longest continuous substring, and this is a substring staggered 01 strings. Xiao Yi need you to help find the length of the longest such substring is.

Enter a description:

Input string including s, s is the length of the length (1 ≤ length ≤ 50), only contains the string '0' and '1'

Output Description:

Output an integer representing the length of the longest substring meet the requirements.
Example 1

Entry

copy
111101111

Export

copy
3
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;
 
  
int main(){
    vector<char> v;
    char k;
    //cin>>n;
    while(cin>>k)
       v.push_back(k);
    int len=1;
    int max=1;
   
 
    for(int j=1;j<v.size();j++)
    {
        if (v [i]! = v [j- 1 ]) 
        { 
            as ++ ; 
            max = (max> only)? max: only;
            continue ; 
        } 
        As = 1 ; 
    } 
  Cout << max << endl; 
  
return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/cstdio1/p/11041045.html