CF-51B - bHTML Tables Analisys(easy+栈)

B - bHTML Tables Analisys
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

In this problem is used an extremely simplified version of HTML table markup. Please use the statement as a formal document and read it carefully.

A string is a bHTML table, if it satisfies the grammar:

TABLE ::= <table>ROWS</table>
ROWS ::= ROW | ROW ROWS
ROW ::= <tr>CELLS</tr>
CELLS ::= CELL | CELL CELLS
CELL ::= <td></td> | <td>TABLE</td>

Blanks in the grammar are only for purposes of illustration, in the given data there will be no spaces. The bHTML table is very similar to a simple regular HTML table in which meet only the following tags : "table", "tr", "td", all the tags are paired and the table contains at least one row and at least one cell in each row. Have a look at the sample tests as examples of tables.

As can be seen, the tables may be nested. You are given a table (which may contain other(s)). You need to write a program that analyzes all the tables and finds the number of cells in each of them. The tables are not required to be rectangular.

Input

For convenience, input data can be separated into non-empty lines in an arbitrary manner. The input data consist of no more than 10 lines. Combine (concatenate) all the input lines into one, to get a text representation s of the specified table. String s corresponds to the given grammar (the root element of grammar is TABLE), its length does not exceed 5000. Only lower case letters are used to write tags. There are no spaces in the given string s.

Output

Print the sizes of all the tables in the non-decreasing order.

Sample Input

Input
<table><tr><td></td></tr></table>
Output
1 


Input
<table>
<tr>
<td>
<table><tr><td></td></tr><tr><td></
td
></tr><tr
><td></td></tr><tr><td></td></tr></table>
</td>
</tr>
</table>
Output
1 4 


Input
<table><tr><td>
<table><tr><td>
<table><tr><td>
<table><tr><td></td><td></td>
</tr><tr><td></td></tr></table>
</td></tr></table>
</td></tr></table>
</td></tr></table>
Output
1 1 1 3 

思路:直接用左右table 来分割计算表格,/td来记录表格大小,至于层数,直接用个栈就可以解决了。算了一道比较简单的水题。可是我却是一直没出,现在想想这题没出,我的收获更大,主要记忆深刻,就是层数问题,我当时一直想用指针来解决,事实证明这行不通,因为可能有多层嵌套,而这却卡了我2个多小时,对于这种题多余的调试是愚蠢的,远不如重新思考,找解题策略来的有价值。而在这题一直卡着,不去做后面的题更是愚蠢的,一道这种题像我如今的水平半小时不出,就是基本上陷入思维困境,思维混乱,这时去解决其他的题目是中明确的选择,等过半小时在来情况会好许多。


#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int mm=27700;
char tt[][40]={"table","/table","tr","/tr","td","/td"};
char s[mm],t[mm];
long long table[mm],num,zk,zr,stak[mm],sta;
int main()
{ int pos=0;
  while(cin>>s)
  {
    pos+=sprintf(t+pos,"%s",s);
  }
  memset(table,0,sizeof(table));
  memset(stak,0,sizeof(stak));sta=-1;
  int i=0,kos=0;num=zk=zr=0;
  for(i=0;i<pos;)
  {
    if(t[i]=='<')
    {   ++i; kos=0;
       //cout<<i<<endl;
        for(;i<pos;++i)
        if(t[i]=='>')
        {s[kos]='\0';++i;break;}
        else
        {
            s[kos]=t[i];
            kos++;
        }

       for(int j=0;j<6;j++)
        if(strcmp(s,tt[j])==0)
       { //cout<<"yes\n";
         if(j==1){zk=stak[--sta];}
         if(j==0){stak[++sta]=num;zk=num;++num;}///左括号
         if(j==5){++table[zk];}
         break;
       }
    }
  }
  ///cout<<z<<num<<endl;
  sort(table,table+num);
  for(int j=0;j<num;++j)
    cout<<table[j]<<" ";
    cout<<"\n";
}




posted @ 2013-03-20 08:19  剑不飞  阅读(255)  评论(0编辑  收藏  举报