SGU 304 解题报告

http://acm.sgu.ru/problem.php?contest=0&problem=302

302. BHTML 1.0

Time limit per test: 2 second(s)
Memory limit: 65536 kilobytes

The hypertext markup language BHTML 1.0 has only two paired tags. They are <UP> </UP> and <DOWN> </DOWN>. The <UP> </UP> tag capitalizes all letters inside its body (between an open tag and a close one), and <DOWN> </DOWN> makes all inside the body letters lowercase. You are given the text consisting of latin letters and tags. Your task is to write the text right as it will be shown in the Bernet Explorer browser window. Tags in the text are arranged correctly, i.e. they form correct bracket sequence. If a letter lays inside several tags, its case is defined by the most inner tag.

Input

The input contains the string S with the text. The length of the string is a natural number not exceeding 1000. Tags are always written in uppercase.

Output

Write to the output text after the processing.

Example(s)

sample input sample output
Thi<UP>sIs<DOWN>EaSY</DOWN>Pr<UP>O</UP>ble</UP>m ThiSISeasyPROBLEm

栈操作,用stack或者用一个数组模拟栈就可以了.因为两者的算法复杂度是一样的并且stack的时间开销不是很大,所以最后运行效率也基本一样.

CODE

#include
#include
#include
#include
int main()
{
  int i=0;
  char s[1005],*idx;
  _Bool stac[1005];
  scanf("%s",s);
  idx=s;
  while(*idx){
      if(*idx=='<'){
          if(*(idx+1)=='/'){
              --i;
              if(*(idx+2)=='U')
                idx+=5;
              else
                idx+=7;
          }else{
              ++i;
              if(*(idx+1)=='U'){
                  stac[i]=true;
                  idx+=4;
              }else{
                  stac[i]=false;
                  idx+=6;
              }
          }
      }else{
          if(i==0) putchar(*idx);
          else if(stac[i]) putchar(toupper(*idx));
          else putchar(tolower(*idx));
          idx++;
      }
  }
  putchar('n');
  return 0;
}
发表评论?

0 条评论。

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>