题解 AT2059 【[AGC005A] STring】

· · 题解

AT2059 【[AGC005A] STring】

\texttt{Update:修改了 \LaTeX}

我一开始是以为搜索 ST 然后删除就可以了,但是没想到竟然 \footnotesize\color{white}\colorbox{#052242}{ \texttt{TLE} } 了.

经过思考,我发现我们只需要用栈即可.

既然题目要求遇到 ST 就删掉,那我们每次读入一个字符,如果是 'T' 且栈顶是 'S' 就弹出. 否则则将该字符压入栈中.

答案就是栈的大小

\huge\tt{Code:}

本质上是一个栈,不过我不喜欢循环,用了递归

众所周知main函数是可以递归的

#include <stdio.h>
char st[200009];
int sz;
int main()
{
    char c=getchar();
    if(c=='\r'||c=='\n')
    {
        printf("%d",sz);
        exit(0); //强行结束程序
    }
    if(sz)
    {
        if(c=='T'&&st[sz-1]=='S')
        {
            st[sz--]=0;
            main();
        }
    }
    st[sz++]=c;
    main();
    return 0;
}

话说这道题用 \tt{KMP} 竟然 \color{white}\colorbox{#052242}{ \texttt{TLE} } 是什么鬼??