Codeforces Gym 100345A BibTeX 模拟

9 篇文章 0 订阅
9 篇文章 0 订阅

题目大意:

给一个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; 以方便排序,用结构体存下每个引用,方便最后写比较函数。

代码:

4552355Accepted48062GNU G++ 4.9.2375455: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;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值