302. BHTML 1.0
Time limit per test: 0.25 second(s)
Memory limit: 65536 kilobytes
Memory limit: 65536 kilobytes
input: standard
output: standard
output: standard
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 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char up(char c){
if('a'<=c&&c<='z')return c-'a'+'A';
return c;
}
char down(char c){
if('A'<=c&&c<='Z')return c-'A'+'a';
return c;
}
char s[1005];
int main(){
scanf("%s",s);
stack<int> st;
int len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]=='<'){
if(s[i+1]=='/'){
st.pop();
if(s[i+1+1]=='U'){
i+=4;
}
else{
i+=6;
}
}
else{
if(s[i+1]=='U'){
st.push(1);
i+=3;
}
else{
st.push(2);
i+=5;
}
}
}
else{
if(st.empty()){
printf("%c",s[i]);
}
else{
if(st.top()==1){
printf("%c",up(s[i]));
}
else{
printf("%c",down(s[i]));
}
}
}
}
return 0;
}

本文介绍了一个简单的BHTML1.0解析器实现,该解析器能够正确处理UP和DOWN标签,使文本中的字母按标签要求转换为大写或小写。通过使用C++标准库中的栈来跟踪标签状态,确保了文本处理的准确性。
877

被折叠的 条评论
为什么被折叠?



