调了半天,总算修好了 TLE 和 MLE,然而 WA了一大片,我自己构造了十几组数据也hack不掉,求调
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
namespace Main
{
const int maxn=105;
int n;
char line[maxn];
unordered_map<string,string> def;
int size_line;
struct Define
{//用来临时存储正在读入宏的信息
char tmp[10];
char name[maxn];
char val[maxn];
}define;
string ans_line;//存储每个蒲通文本宏展开后的样子
string temp;
unordered_map<string,bool> vis;
bool isletter(char s)
{
if(s-'a'>=0&&s-'a'<26)return 1;
if(s-'A'>=0&&s-'A'<26)return 1;
return 0;
}
string dfs(string hong)
{
string ttttmp=hong;
hong=def[hong];
if(vis[ttttmp])return ttttmp;
vis[ttttmp]=1;
string str="";
string temp="";
for(int i=0;i<hong.size();i++)
{
if(hong[i]=='\n')break;
// cout<<"temp: "<<temp<<endl;
if(isletter(hong[i]))
{
temp+=hong[i];
}
else
{
if(def.count(temp))
{//连续字母结束,处理一下宏
//不要忘记读入完整个字符串之后还要展开一下temp
str+=dfs(temp);
}
else
{
str+=temp;
}
temp=hong[i];
if(def.count(temp))
{//如果当前文本是一个宏,替换一下
str+=dfs(temp);
}
else
{//不是宏,不做处理
str+=temp;
}
temp="";
}
}
if(def.count(temp))
{
str+=dfs(temp);
}
else
{
str+=temp;
}
vis[ttttmp]=0;
return str;
}
void main()
{
cin>>n;
getchar();
while(n--)
{
fgets(line, maxn, stdin);
size_line=strlen(line);
if(line[0]=='#')
{//预处理指令
sscanf(line,"%s %s %s",define.tmp,define.name,define.val);
int tttmp=strlen(define.val);
int ttmp=strlen(define.tmp)+strlen(define.name)+tttmp+3;
if(ttmp!=size_line)
{
for(int i=ttmp-1,cnt=0;i<size_line;i++,cnt++)
{
if(line[i]=='\n')break;
define.val[tttmp+cnt]=line[i];
}
}
if(define.tmp[1]=='d')
{
def[define.name]=define.val;
}
if(define.tmp[1]=='u')
def.erase(define.name);//#undef
putchar('\n');
continue;
}
else
{//蒲通文本
ans_line="";
temp="";
for(int i=0;i<size_line;i++)
{
if(line[i]=='\n')break;
// cout<<"temp: "<<temp<<endl;
if(isletter(line[i]))
{
temp+=line[i];
}
else
{
if(def.count(temp))
{//连续字母结束,处理一下宏
//不要忘记读入完整个字符串之后还要展开一下temp
ans_line+=dfs(temp);
}
else
{
ans_line+=temp;
}
temp=line[i];
if(def.count(temp))
{//如果当前文本是一个宏,替换一下
ans_line+=dfs(temp);
}
else
{//不是宏,不做处理
ans_line+=temp;
}
temp="";
}
}
if(def.count(temp))
{
ans_line+=dfs(temp);
}
else
{
ans_line+=temp;
}
cout<<ans_line<<endl;
}
}
scanf("%s",line);
}
}
int main()
{
Main::main();
return 0;
}