Skip to content

Instantly share code, notes, and snippets.

@rogerioagjr
Created June 8, 2016 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerioagjr/3cb20e7a136c7410ebadfda7cc4dc69c to your computer and use it in GitHub Desktop.
Save rogerioagjr/3cb20e7a136c7410ebadfda7cc4dc69c to your computer and use it in GitHub Desktop.
Chaves
// Chaves - F1P2 - OBI 2016
// Rogério Júnior
// Complexidade: O(entrada)
#include <cstdio>
int qtd;
bool erro;
char c;
int main(){
// leio todos os caracteres da entrada
while(scanf("%c", &c)!=EOF){
// se for uma chave abrindo
if(c=='{') qtd++; // incremento o valor de qtd em 1 unidade
// se for uma chave fechando
if(c=='}'){
// checo se há chaves abertas
if(qtd<1) erro=true;
// e diminuo o valor de qtd em 1 unidade
qtd--;
}
}
// se fechei chaves que não haviam sido abertas ou ainda há chaves abertas
if(erro||qtd) printf("N\n"); // há erro de compilação
// caso contrário, não há erro nas chaves do código
else printf("S\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment