Parentheses Balance UVA - 673(括号匹配栈)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/leekerian/article/details/86776519
#include <iostream>
#include <cstring>
#include <string>

using namespace std;


const int MAXN=111111;
string str[MAXN];

int cont=0;
bool islable( char c ) {
    if( c == ' ' || c  == '|' || c == '#' || c == '-' || c == '/n' )
    return false ;
    return true ;
}
void dfs(int l,int r,int c)
{
  if(c>=cont)return ;
  for(int i=l;i<=r&&i<str[c].size();i++)
  {
    if(islable(str[c][i]))
    {
      cout<<str[c][i]<<"(";
      if(c<cont-1)
      {
        if(str[c+1][i]=='|')
        {
          int j=i;
          while(str[c+2][j]=='-')
            j--;
          j++;
          int z=j;
          while(str[c+2][z]=='-')
            z++;
          z--;
          dfs(j,z,c+3);
        }
      }
      cout<<")";
    }
  }
}
int main()
{
  int n;
  cin>>n;
  getchar();
  while(n--)
  {
    cont=0;
    while(getline(cin,str[cont++]))
      if(str[cont-1][0]=='#') break;
    cout<<"(";
    dfs(0,str[0].size()-1,0);
    cout<<")"<<endl;;
  }  
  return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/86776519
今日推荐