啊!猪国杀太难了!写一道大模拟吧!
Day1:20200814
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(getline(cin, s))
{
bool lh1, lh2, rh1, rh2; //判断是否匹配
lh1 = lh2 = rh1 = rh2 = 0; //初始化为0
int len = s.length();
for(int i = 0; i < len; i++) //先判断是否合法
{
if(s[i] == '[' && s[i + 1] == 'h' && s[i + 2] == '1' && s[i + 3] == ']')
{
lh1 = 1; //当看到[h1]字样时,lh1=1
}
if(s[i] == '[' && s[i + 1] == 'h' && s[i + 2] == '2' && s[i + 3] == ']')
{
lh2 = 1; //当看到[h2]字样时,lh2=1
}
if(s[i] == '[' && s[i + 1] == '/' && s[i + 2] == 'h' && s[i + 3] == '1' && s[i + 4] == ']')
{
rh1 = 1; //当看到[/h1]字样时,rh1=1
}
if(s[i] == '[' && s[i + 1] == '/' && s[i + 2] == 'h' && s[i + 3] == '2' && s[i + 4] == ']')
{
rh2 = 1; //当看到[/h1]字样时,rh1=1
}
}
//printf("\n%d %d %d %d\n", lh1, lh2, rh1, rh2);
if(lh1 && !rh1 || !lh1 && rh1 || lh2 && !rh2 || !lh2 && rh2)
{
printf("%s", "Unclosed Mark");
return 0;
}
for(int i = 0; i < len; i++) //再输出
{
if(s[i] == '[' && s[i + 1] == 'h' && s[i + 2] == '1' && s[i + 3] == ']')
{
printf("%c ", '#');
for(int j = i + 4; s[j] != '['; j++)
{
printf("%c", s[j]);
}
}
if(s[i] == '[' && s[i + 1] == 'h' && s[i + 2] == '2' && s[i + 3] == ']')
{
printf("%s", "## ");
for(int j = i + 4; s[j] != '['; j++)
{
printf("%c", s[j]);
}
}
if(s[i] == '[' && s[i + 1] == '/' && s[i + 2] == 'h' && s[i + 3] == '1' && s[i + 4] == ']')
{
printf(" %c", '#');
break;
}
if(s[i] == '[' && s[i + 1] == '/' && s[i + 2] == 'h' && s[i + 3] == '2' && s[i + 4] == ']')
{
printf("%s", " ##");
break;
}
}
}
}OK,20分拿下