[C++] 백준 5076번: Web Pages

2022. 7. 13. 19:34·problem solving/백준

문제

https://www.acmicpc.net/problem/5076

 

5076번: Web Pages

Input will consist of a number of lines of HTML code, each line containing from 0 to 255 characters. The last line will contain a single # character – do not process this line. Within the text of each line will be zero or more tags. No angle bracket will

www.acmicpc.net

 

풀이

파싱하는게 제일 어려운 문제로 괄호 문제랑 비슷하게 풀었다. 

 

<tag> text </tag> 를 예시로 들겠다.

  • <tag>는 스택에 넣어준다. 
  • </tag>는 스택의 top과 비교한다. 
    •  top과 같다면 짝이 맞게 들어간 태그고 다르다면 짝지 맞지 않다.
  • <br />과 <a href=> </a>은 따로 처리 해주었다. (소스 참고)

 

+

 

한 줄 전체를 받기 위해서 std::cin이 아닌 std::getline(std::cin, v)를 사용해주었다.

소스

#include <iostream>
#include <string>
#include <stack>
int main()
{
    
    std::string text;
    while(1)
    {
        bool isLegal = true;
        std::stack<std::string> tags;
        std::getline(std::cin, text);
        if(text == "#") break;
        
        for(int i = 0; i<text.length(); ++i)
        {
            auto c = text[i];
            
            if(c == '<')
            {
                std::string tagname;
                for(int j = i+1; j<text.length(); ++j)
                {
                    if(text[j]=='>')
                    {
                        //</tag>
                        if((text.length() > i + 1) && text[i+1]=='/')
                        {
                            tagname = std::string(text.begin() + i+2, text.begin() + j);
                            
                            if((tags.empty() == false) && (tagname == tags.top()))
                                tags.pop();
                            else
                                isLegal = false;
                        }
                        else
                        {
                            
                            // not <br />
                            if(text[j-1] != '/')
                            {
                                //<a locat = ">
                                if((text.length() > i + 2 )&& text[i+2] == ' ')
                                    tagname = std::string(text.begin() + i+1, text.begin() + 2);
                                else
                                    tagname = std::string(text.begin() + i+1, text.begin() + j);
                                
                                tags.push(tagname);
                            }
                            
                        }
                        break;
                    }
                }
                
                if(isLegal == false)
                    break;
                
            }
            
        }
        // 스택에 tag가 남아있다면 짝이 맞지 않은 것이다.
        if(isLegal || tags.empty())
            std::cout << "legal\n";
        else
            std::cout <<"illegal\n";
        
    }//while
    return 0;
}
 
Colored by Color Scripter
cs
저작자표시 비영리 변경금지 (새창열림)
'problem solving/백준' 카테고리의 다른 글
  • [C++] 백준 1697번: 숨바꼭질
  • [C++] 백준 1260번: DFS와 BFS
  • [C++] 백준 1406번: 에디터
  • [C++] 백준 5397번: 키로거
u1qns
u1qns
http://github.com/u1qns
  • u1qns
    개발 블로그
    u1qns
  • 전체
    오늘
    어제
    • 분류 전체보기 (173)
      • 회고 (1)
      • programming (17)
        • 개념 정리 (6)
        • CI CD (1)
        • 트러블 슈팅 (0)
        • 환경설정 및 팁 (7)
      • problem solving (155)
        • 개념 정리 (3)
        • 백준 (129)
        • SWEA (15)
        • 프로그래머스 (4)
  • 블로그 메뉴

    • 홈
    • 방명록
  • 링크

    • 깃허브
  • 공지사항

  • 인기 글

  • 태그

    완전탐색
    BFS
    boj
    DP
    투포인터
    SSAFY
    HELLOSSAFY
    그리디
    되추적
    cpp
    삼성청년SW아카데미
    SSAFY수료식
    미해결
    구현
    C++
    백준
    SWEA
    cmath
    POW
    DFS
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
u1qns
[C++] 백준 5076번: Web Pages
상단으로

티스토리툴바