Markdown编辑器
第十次CCF-CSP计算机软件能力认证
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
string get(string t)
{
int pos = 1;
while (t[pos] == ' ') pos ++ ;
string res;
int sz = t.size();
while (pos < sz) res += t[pos], pos ++ ;
return res;
}
int find(string str, string t)
{
int sz = str.size();
for (int i = 0; i < sz; i ++ )
if (str[i] == t[0])
return 0;
return -1;
}
void change1(string& t)
{
while (find(t, "_") != -1)
{
int pos = 0;
while (t[pos] != '_') pos ++ ;
t.erase(pos, 1);
t.insert(pos, "<em>");
pos = 0;
while (t[pos] != '_') pos ++ ;
t.erase(pos, 1);
t.insert(pos, "</em>");
}
}
void change2(string& t)
{
while (find(t, "[") != -1)
{
int pos = 0, st, ed;
while (t[pos] != '[') pos ++ ;
st = pos;
string res = "<a href=\"";
string Text, Link;
pos ++ ;
while (t[pos] != ']') Text += t[pos], pos ++ ;
pos += 2;
while (t[pos] != ')') Link += t[pos], pos ++ ;
ed = pos;
t.erase(st, ed - st + 1);
t.insert(st, res + Link + "\">" + Text + "</a>");
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
string t;
while (getline(cin, t))
{
while (!t.size())
if (!getline(cin, t)) return 0;
change1(t), change2(t);
if (t[0] == '#')
{
int sz = t.size();
int pos = 1;
int cnt = 1;
while (t[pos] == '#') pos ++, cnt ++ ;
while (t[pos] == ' ') pos ++ ;
string res = "";
while (pos < sz) res += t[pos], pos ++ ;
cout << "<h" << cnt << '>' << res << "</h" << cnt << ">\n";
if (!getline(cin, t)) return 0;
}
else if (find(t, "#") == -1 && find(t, "*") == -1)
{
cout << "<p>" << t;
if (!getline(cin, t)) return cout << "</p>\n", 0;
if (t.size()) cout << '\n';
while (t.size())
{
change1(t), change2(t);
cout << t;
if (!getline(cin, t)) return cout << "</p>\n", 0;
if (t.size()) cout << '\n';
}
cout << "</p>\n";
}
else if (t[0] == '*')
{
cout << "<ul>\n";
string str = get(t);
cout << "<li>" << str << "</li>\n";
if (!getline(cin, t)) return cout << "</ul>\n", 0;
while (t.size())
{
change1(t), change2(t);
str = get(t);
cout << "<li>" << str << "</li>\n";
if (!getline(cin, t)) return cout << "</ul>\n", 0;
}
cout << "</ul>\n";
}
}
return 0;
}
JSON查询
第十一次CCF-CSP计算机软件能力认证
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
int n, m;
string s;
void change(string& t)
{
int pos = 0, sz = t.size();
while (pos < sz)
{
if (t[pos] == '\"' || t[pos] == '\\')
{
t.insert(pos, "\\");
pos ++ ;
}
pos ++ ;
sz = t.size();
}
}
bool last(string str, int pos)
{
while (pos)
{
pos -- ;
if (str[pos] == ':') return false;
if (str[pos] != ' ') return true;
}
return true;
}
int main()
{
scanf("%d%d", &n, &m);
n ++ ;
string t;
while (n -- )
{
getline(cin, t);
s += t;
}
while (m -- )
{
getline(cin, t);
change(t);
string str = s.substr(1, s.size() - 2);
int pos = 0, sz = t.size();
string g;
bool success = true;
while (pos < sz)
{
g = "";
while (pos < sz && t[pos] != '.')
g += t[pos], pos ++ ;
pos ++ ;
int siz = str.size();
int cnt = 0;
int P = 0;
int first = -1;
unordered_map<string, int> mp;
int sum = 0;
while (P < siz)
{
if (str[P] == '\\') cnt ^= 1;
if (str[P] == '\"')
{
if (!cnt)
{
if (first == -1) first = P + 1;
else
{
if (!sum && last(str, first - 1)) mp[str.substr(first, (P - 1) - first + 1)] = first;
first = -1;
}
}
else cnt ^= 1;
}
if (first == -1 && str[P] == '{') sum ++ ;
if (first == -1 && str[P] == '}') sum -- ;
P ++ ;
}
if (!mp.count(g))
{
success = false;
break;
}
else
{
int first = mp[g] + g.size(), end;
first += 2;
while (str[first] != '{' && str[first] != '\"') first ++ ;
end = first + 1;
if (str[first] == '{')
{
int cnt = 1;
while (str[end] != '}' || cnt > 1)
{
if (str[end] == '{') cnt ++ ;
if (str[end] == '}') cnt -- ;
if (str[end] == '\"')
{
int f = 0;
end ++ ;
while (str[end] != '\"' || f)
{
if (str[end] == '\"') f ^= 1;
if (str[end] == '\\') f ^= 1;
end ++ ;
}
}
end ++ ;
}
}
else
{
int cnt = 0;
while (str[end] != '\"' || cnt)
{
if (str[end] == '\\') cnt ^= 1;
if (str[end] == '\"') cnt ^= 1;
end ++ ;
}
}
str = str.substr(first, end - first + 1);
}
if (pos < sz) str = str.substr(1, str.size() - 2);
}
if (!success) cout << "NOTEXIST\n";
else if (str[0] == '{') cout << "OBJECT\n";
else
{
int pos = 0, sz = str.size();
while (pos < sz)
{
if (str[pos] == '\\') str.erase(pos, 1);
pos ++ ;
sz = str.size();
}
cout << "STRING " << str.substr(1, str.size() - 2) << '\n';
}
}
return 0;
}