题目链接:https://codeforces.com/contest/1090/problem/B

Codeforces 1090B - LaTeX Expert - [字符串模拟][2018-2019 Russia Open High School Programming Contest Problem B]-LMLPHP

Codeforces 1090B - LaTeX Expert - [字符串模拟][2018-2019 Russia Open High School Programming Contest Problem B]-LMLPHP

Codeforces 1090B - LaTeX Expert - [字符串模拟][2018-2019 Russia Open High School Programming Contest Problem B]-LMLPHP

Examples
standard input

The most famous characters of Pushkin’s works are Onegin \cite{onegin},
Dubrovsky \cite{dubrovsky} and Tsar Saltan \cite{saltan}.
\begin{thebibliography}{99}
\bibitem{saltan} A.S.Pushkin. The Tale of Tsar Saltan. 1832.
\bibitem{onegin} A.S.Pushkin. Eugene Onegin. 1831.
\bibitem{dubrovsky} A.S.Pushkin. Dubrovsky. 1841.
\end{thebibliography}

standard output

Incorrect
\begin{thebibliography}{99}
\bibitem{onegin} A.S.Pushkin. Eugene Onegin. 1831.
\bibitem{dubrovsky} A.S.Pushkin. Dubrovsky. 1841.
\bibitem{saltan} A.S.Pushkin. The Tale of Tsar Saltan. 1832.
\end{thebibliography}

standard input

The most famous characters of Pushkin’s works are Onegin \cite{onegin},
Dubrovsky \cite{dubrovsky} and Tsar Saltan \cite{saltan}.
\begin{thebibliography}{99}
\bibitem{onegin} A.S.Pushkin. Eugene Onegin. 1831.
\bibitem{dubrovsky} A.S.Pushkin. Dubrovsky. 1841.
\bibitem{saltan} A.S.Pushkin. The Tale of Tsar Saltan. 1832.
\end{thebibliography}

standard output

Correct

题意:

文章末尾这个参考文献的顺序可能是错乱的,需要你修正一下。

题解:

就是考察字符串输入输出以及其他一些操作的,用map<string,int>哈希一下参考文献的标识,然后老老实实模拟就行了。

AC代码:

#include<bits/stdc++.h>
using namespace std; struct Cite{
int idx;
string str;
Cite(){}
Cite(int _i,const string& s) {
idx=_i, str=s;
}
bool operator<(const Cite& oth)const {
return idx<oth.idx;
}
};
vector<Cite> cites; int tot;
map<string,int> mp; void Find(const string& s)
{
int pos,beg=;
while((pos=s.find("\\cite{",beg))!=-)
{
string res;
for(beg=pos+;beg<s.size() && s[beg]!='}';beg++) res+=s[beg];
mp[res]=++tot;
}
}
int main()
{
ios::sync_with_stdio();
cin.tie(), cout.tie(); string str;
bool END=;
tot=; mp.clear();
while(getline(cin,str))
{
if(str=="\\begin{thebibliography}{99}") {END=;continue;}
if(str=="\\end{thebibliography}") break;
if(!END) Find(str);
else
{
int pos=str.find("\\bibitem{");
string res;
for(pos+=;pos<str.size() && str[pos]!='}';pos++) res+=str[pos];
cites.push_back(Cite(mp[res],str));
}
} bool CORRECT=;
for(int i=;i<cites.size();i++) if(cites[i].idx!=i+) CORRECT=;
if(CORRECT) cout<<"Correct\n";
else
{
cout<<"Incorrect\n";
sort(cites.begin(),cites.end());
cout<<"\\begin{thebibliography}{99}\n";
for(int i=;i<cites.size();i++) cout<<cites[i].str<<'\n';
cout<<"\\end{thebibliography}\n";
}
}
05-07 15:36