2015年2月2日月曜日

Lesson 5 : Brackets (Brackets)

Lesson 5 : Brackets
https://codility.com/programmers/lessons/5

This is a simple problem to check the matchings of brackets. 
Here is the simplest solution. 

Simply, we put the left bracket characters onto the stack when any of them is found. When any right bracket is found, we check if the character (left bracket) on the stack top matches to the right bracket.

One thing that should not be forgotten is see if the string is correctly terminated with no left bracket on the stack.

This solution gives the 100% score.











#include <alloca.h>

int solution(char *S) {
    
    int len = strlen(S);
    
    if (len == 0){
        return 1;
    }
    
    //I always wonder why thye don't tell anything about
    //what we shoud do when memory allocation failed 
    //in codility problems...
    char*   stack = (char*)alloca(len);
  
  
    int idx = 0;  //this is a stack pointer.
    
    int i;
    //scan the character one-by-one
    for (i = 0; i < len; i++){
        char c = S[i];
        
        switch(c){
            //if the character is '(', '[', '{',
            //just push it onto the stack.
            case '(':
            case '[':
            case '{':
                stack[idx] = c;
                idx++;
                break;
                
            //if the character is '(', '[', '{',
            //check if the last character matches with each
            case ')':
                idx--;
                if (stack[idx] != '('){
                    return 0;
                }
                break;
            case ']':
                idx--;
                if (stack[idx] != '['){
                    return 0;
                }
                break;
            case '}':
                idx--;
                if (stack[idx] != '{'){
                    return 0;
                }
                break;
            default:
                return 0;
        }
    }
    
    //we need to see if the string is terminted collectly
    if (idx != 0){
        return 0;
    }
    
    return 1;
}

1 件のコメント:

  1. Following having a few times with mid-major March Madness championship game titles

    claiming the schoolMarch Madness

    Live
    hockey spotlight, the Power 5 March Madness Live Stream conference tournaments commence to take center stage today. These types of are the games that

    will help define the way the bubble and the returning end of the NCAA

    March Madness Bracket Tournament bracket shakes away.
    March Madness 2017
    On Wednesday, Syracuse, Ohio, March

    Madness 2017 Live
    California, TCU, Ohio Point out, Wake Forest, 2017 March Madness Virginia Technology, Texas

    Tech, Xavier and El monte all play game titles NCAA March Madness that will either help solidify their

    at-large berth/seed or make Selection Weekend more of an

    troubled day. march madness schedule

    One other quick note: I moved Gonzaga back up to the top seed line after a third thoroughly

    impressive win over a very good Saint Mary's team in the WCC subject game. That's not

    total, though. The Pac-12 competition champion march madness schedule 2017 could easily find

    yourself there by the end of the week, particularly if the winner of the

    Arizona/UCLA semifinal beats Or in it game.

    返信削除