多余括号消除,找测试数据看看就知道哪些括号要消除了
http://icpc.sharif.edu/archive/
#include<stdio.h>
#include<string.h>
#include<ctype.h>
static char prev(char *s, int index)
{
while (index && s[index - 1] == ' ')
index--;
if (!index)
return -1;
else
return s[index - 1];
}
int main()
{
int t, map[256], stack[20], index;
scanf("%d", &t);
getchar();
char s[256];
while (t--)
{
gets(s);
memset(map, 0, sizeof(map));
index = 0;
int i, j;
for (i = 0; s[i] != '\0'; i++)
if (s[i] == '(')
stack[index++] = i;
else if (s[i] == ')')
map[stack[--index]] = i;
for (i = 0; i < 256; i++)
if (map[i])
{
int temp = 0;
for (j = i + 1; j < map[i]; j++)
if (isupper(s[j]))
temp++;
if (temp == 1)
{
s[i] = ' ';
s[map[i]] = ' ';
}
char c = prev(s, i);
if (c == -1 || c == '+' || c == '(')
{
s[i] = ' ';
s[map[i]] = ' ';
}
}
for (i = 0; s[i] != '\0'; i++)
if (s[i] != ' ')
putchar(s[i]);
putchar('\n');
}
return 0;
}