I have been solving some UVa problems. 673 - Parentheses Balance is one of them. But I got WA. I have no idea about what I did wrong. I checked my code several times, but I have no clue where did I messed up. I also checked on uDebug. For all test cases, I have got the correct output. But when I submit my code, I got WA. Please help me. I am new in Programing.
Here is my code. Thank You.
#include <bits/stdc++.h>
using namespace std;
bool checkBalance(char *line)
{
int arr[129], pointer = 0;
for (int i = 0; line[i] != '\n'; i++)
{
if (line[i] == '(')
arr[pointer++] = 1;
else if (line[i] == ')' && arr[pointer - 1] == 1 && i)
arr[--pointer] = 0;
else if (line[i] == '{')
arr[pointer++] = 2;
else if (line[i] == '}' && arr[pointer - 1] == 2 && i)
arr[--pointer] = 0;
else if (line[i] == '[')
arr[pointer++] = 3;
else if (line[i] == ']' && arr[pointer - 1] == 3 && i)
arr[--pointer] = 0;
else if (line[i] != '(' && line[i] != ')' && line[i] != '[' && line[i] != ']')
continue;
else
return false;
}
if (!pointer)
return true;
else
return false;
}
int main()
{
//freopen("input.txt", "r", stdin);
int test;
scanf("%d", &test);
getchar();
while (test--)
{
char ch[129];
fgets(ch, sizeof(ch), stdin);
if (checkBalance(ch))
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire