题目大意:
给一个BibTex的代码,要求你将引用格式化
Sample:
Input
@book
{
author = “Donald Ervin Knuth”,
title = “The Art of Computer Programming”,
volume = “1”,
publisher = “Addison-Wesley Professional”,
year = “1997”
}
@book
{
author = “Donald Ervin Knuth”,
title = “The Art of Computer Programming”,
volume = “2”,
publisher = “Addison-Wesley Professional”,
year = “1997”
}
@article
{
author = “Robert Endre Tarjan and Andrew Goldberg”,
title = “A new approach to the maximum flow problem”,
journal = “Journal ACM”,
volume = “35”,
year = “1988”,
pages = “921–940”
}
Output
[1] Goldberg A., Tarjan R. E. A new approach to the maximum flow problem // Journal ACM, 35 – 1988 – pp. 921–940
[2] Knuth D. E. The Art of Computer Programming, Vol. 1 – Addison-Wesley Professional, 1997
[3] Knuth D. E. The Art of Computer Programming, Vol. 2 – Addison-Wesley Professional, 1997
做法:
这题重点有两个,一是读入处理,二是排序。
- 读入:我们可以一次将一个应用全部读入到一个字符串中,然后在字符串中进行处理。比较合理的做法是找’=’号再分别处理两边。但是本题数据水可以直接对每个项目直接查找。
- 排序:可使用vector保存人名vector<vector<string>>author;以方便排序,用结构体存下每个引用,方便最后写比较函数。
代码:
| 4552355 | Accepted | 480 | 62 | GNU G++ 4.9.2 | 3754 | 55:08:28 |
|---|
#include <bits/stdc++.h>
using namespace std;
const int maxn=1000;
string buf,tmp;
struct info{
bool type;
vector<vector<string> >author;
string title;
string pub_jour;
int year;
int vol;
int num;
string pages;
void get_type()
{
if(buf.find("book") <=2 )type=1;
else type=0;
}
void get_author()
{
vector<string>t;
int pos=buf.find("author");
int st=pos;
while(buf[st]!='"')++st;
int ed=st+1;
while(buf[ed]!='"')++ed;
tmp="";
for(int i=st+1;i<=ed;i++)
{
if(buf[i] != ' ' && buf[i] != '"')
tmp+=buf[i];
else
{
if(tmp == "and")
{
tmp=*(--t.end());
t.insert(t.begin(),tmp);
t.resize(t.size()-1);
author.push_back(t);
t.clear();
}
else t.push_back(tmp);
tmp="";
}
}
tmp=*(--t.end());
t.insert(t.begin(),tmp);
t.resize(t.size()-1);
author.push_back(t);
if(author.size()>1)sort(author.begin(),author.end());
}
void get_title()
{
int pos=buf.find("title");
while(buf[pos]!='"')++pos;
while(buf[++pos]!='"')title+=buf[pos];
}
void get_pub_jour()
{
int pos=buf.find(type ? ("publisher") : ("journal") );
while(buf[pos]!='"')++pos;
while(buf[++pos]!='"')pub_jour+=buf[pos];
}
void get_year()
{
tmp="";
int pos=buf.find("year");
while(buf[pos]!='"')++pos;
while(buf[++pos]!='"')tmp+=buf[pos];
for(int i=0;i<(int)tmp.size();i++)year=year*10+tmp[i]-'0';
}
void get_volume()
{
int pos=buf.find("volume");
if(pos == (int)string::npos){vol=-1;return;}
tmp="";
while(buf[pos]!='"')++pos;
while(buf[++pos]!='"')tmp+=buf[pos];
for(int i=0;i<(int)tmp.size();i++)vol=vol*10+tmp[i]-'0';
}
void get_number()
{
if(type){num=-1;return;}
int pos=buf.find("number");
if(pos == (int)string::npos){num=-1;return;}
tmp="";
while(buf[pos]!='"')++pos;
while(buf[++pos]!='"')tmp+=buf[pos];
for(int i=0;i<(int)tmp.size();i++)num=num*10+tmp[i]-'0';
}
void get_pages()
{
if(type)return;
int pos=buf.find("pages");
if(pos == (int)string::npos)return;
while(buf[pos]!='"')++pos;
while(buf[++pos]!='"')pages+=buf[pos];
}
void print()
{
for(int i=0;i<(int)author.size();i++)
{
if(i>0)printf(", ");
for(int j=0;j<(int)author[i].size();j++)
{
if(j==0)printf("%s",author[i][j].c_str());
else printf("%c.",author[i][j][0]);
if(j!=(int)author[i].size()-1)putchar(' ');
}
}
printf(" %s",title.c_str());
if(type)
{
if(vol != -1)printf(", Vol. %d",vol);
printf(" -- %s, %d",pub_jour.c_str(),year);
}else
{
printf(" // %s",pub_jour.c_str());
if(vol != -1)printf(", %d",vol);
if(num != -1)printf(" (%d)",num);
printf(" -- %d",year);
if(pages != "")
printf(" -- %s. %s",
(pages.find("--") == string::npos) ? ("p") : ("pp"),
pages.c_str());
}
}
}bt[maxn];
bool ReadJson()
{
char ch;
do{ch=getchar();}while(ch!='@' && (!feof(stdin) ) );
if(feof(stdin))return false;
buf="";
do{
ch=getchar();
buf+=ch;
}while(ch!='}');
return true;
}
bool cmp(const info &a,const info &b){
if(a.author != b.author)return a.author<b.author;
if(a.title != b.title)return a.title<b.title;
return a.vol<b.vol;
}
int main()
{
int ct=0;
freopen("bibtex.in","r",stdin);
freopen("bibtex.out","w",stdout);
while(ReadJson())
{
bt[ct].get_type();
bt[ct].get_author();
bt[ct].get_title();
bt[ct].get_pub_jour();
bt[ct].get_year();
bt[ct].get_volume();
bt[ct].get_number();
bt[ct].get_pages();
++ct;
}
sort(bt,bt+ct,cmp);
for(int i=0;i<ct;i++)
{
printf("[%d] ",i+1);
bt[i].print();
putchar('\n');
}
return 0;
}

本文介绍了一种处理BibTeX引用格式的方法,包括读取输入、解析引用字段及按作者和标题排序输出的实现细节。

771

被折叠的 条评论
为什么被折叠?



