2013-team5/andrew/30/A

从 Trac 迁移的文章

这是从旧校内 Wiki 迁移的文章,可能存在一些样式问题,您可以向 memset0 反馈。

原文章内容如下:

{{{
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <cassert>

#define foreach(it,v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define sqr(a) ((a)*(a)) 
#define MP(a,b) make_pair((a),(b))

template <class T> void Cmin(T &t,T x){if (x < t) t = x;}
template <class T> void Cmax(T &t,T x){if (x > t) t = x;}

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;

const double PI = acos(-1.0);
const double EPS = 1e-9;
const int INF = 0x3fffffff;

class point
{
public:
    int x, y;
    point(){}
    point(int xx, int yy) : x(xx), y(yy){}
};

struct node{
    int type;
    vector< vector<string> > author;
    string title;
    string journal; //publisher;
    int year;
    int volume;
    int number;
    vector<string> pages;
};

string head, str, line, col;

vector<node> Q;

string gao(string str) // ""
{
    int L = -1, R = 0;
    for (int i = 0; i < str.length(); i++){
        if (L == -1 && str[i] == '"') L = i;
        if (str[i] == '"') R = i;
    }
    return str.substr(L + 1, R - L - 1);
}

int get(string str)
{
    int res = 0;
    for (int i = 0; i < str.length(); i++){
        res = res * 10 + str[i] - '0';
    }
    return res;
}

vector<string> split(string S, string T)
{
    vector<string> Q;
    S += T;
    int pre = 0;
    for (int i = 0; i < S.length(); i++){
        if (S.substr(i, T.length()) == T){
            Q.push_back(S.substr(pre, i - pre));
            pre = i + T.length();
        }
    }
    return Q;
}

bool cmp(const node &a, const node &b)
{
    if (a.author < b.author) return 1;
    if (a.author > b.author) return 0;
    if (a.title < b.title) return 1;
    if (a.title > b.title) return 0;
    return a.volume < b.volume;
}

int main()
{
    //ios::sync_with_stdio(false);
    freopen("bibtex.in", "r", stdin);
    freopen("bibtex.out", "w", stdout);
    while(cin >> head){
        cin >> col; //"{"
        node now;
        now.volume = 0;
        now.number = 0;
        now.pages.clear();
        if (head == "@book") now.type = 2;
        if (head == "@article") now.type = 1;
        while(cin >> str && str != "}"){
            if (str == ",") continue;
            cin >> col; // "="
            char ch;
            while (1) {
                ch = getchar();
                if (ch == '"') break;
            }
            char st[2000];
            scanf("%[^\"]", st); getchar();
            //getline(cin, line);
            line = (string)st;
            //cout << line << endl;
            if (str == "author"){
                //and
                vector<string> temp = split(line, " and ");
                now.author.clear();
                for (int i = 0; i < temp.size(); i++){
                    now.author.push_back(split(temp[i], " "));
                    vector<string> &vs = now.author.back();
                    rotate(vs.begin(), vs.begin() + vs.size() - 1, vs.end());
                }
                sort(now.author.begin(), now.author.end());
            }
            if (str == "title"){
                now.title = line;
            }
            if (str == "journal" || str == "publisher"){
                // a & ' ' & 1 & -
                now.journal = line;
            }
            if (str == "year"){
                now.year = get(line);
            }
            if (str == "volume"){
                now.volume = get(line);
            }
            if (str == "number"){
                now.number = get(line);
            }
            if (str == "pages"){
                now.pages = split(line, "--");
            }
        }
        Q.push_back(now);
    }
    sort(Q.begin(), Q.end(), cmp);
    for (int i = 0; i < Q.size(); i++){
        //printf("%d\n", Q[i].volume);
        printf("[%d] ", i + 1);
        for (int j = 0; j < Q[i].author.size(); j++){
            if (j != 0) printf(", ");
            vector<string> &ss = Q[i].author[j];
            printf("%s", ss[0].c_str());
            for (int k = 1; k < ss.size(); k++){
                printf(" %c.", ss[k][0]);
            }
        }
        printf(" %s", Q[i].title.c_str());
        if (Q[i].type == 1){
            printf(" // %s", Q[i].journal.c_str());
            if (Q[i].volume != 0){
                printf(", %d", Q[i].volume);
            }
            if (Q[i].number != 0){
                printf(" (%d)", Q[i].number);
            }
            printf(" -- %d", Q[i].year);
            if (Q[i].pages.size() == 1){
                printf(" -- p. %s", Q[i].pages[0].c_str());
            }

            if (Q[i].pages.size() == 2){
                printf(" -- pp. %s--%s", Q[i].pages[0].c_str(), Q[i].pages[1].c_str());
            }
            puts("");
        }
        if (Q[i].type == 2){    
            if (Q[i].volume != 0){
                printf(", Vol. %d", Q[i].volume);
            }
            printf(" -- %s, %d", Q[i].journal.c_str(), Q[i].year);
            puts("");
        }
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
}}}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <cassert>
#define foreach(it,v) for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define sqr(a) ((a)*(a)) 
#define MP(a,b) make_pair((a),(b))
template <class T> void Cmin(T &t,T x){if (x < t) t = x;}
template <class T> void Cmax(T &t,T x){if (x > t) t = x;}
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int> VI;
const double PI = acos(-1.0);
const double EPS = 1e-9;
const int INF = 0x3fffffff;
class point
{
public:
    int x, y;
    point(){}
    point(int xx, int yy) : x(xx), y(yy){}
};
struct node{
    int type;
    vector< vector<string> > author;
    string title;
    string journal; //publisher;
    int year;
    int volume;
    int number;
    vector<string> pages;
};
string head, str, line, col;
vector<node> Q;
string gao(string str) // ""
{
    int L = -1, R = 0;
    for (int i = 0; i < str.length(); i++){
        if (L == -1 && str[i] == '"') L = i;
        if (str[i] == '"') R = i;
    }
    return str.substr(L + 1, R - L - 1);
}
int get(string str)
{
    int res = 0;
    for (int i = 0; i < str.length(); i++){
        res = res * 10 + str[i] - '0';
    }
    return res;
}
vector<string> split(string S, string T)
{
    vector<string> Q;
    S += T;
    int pre = 0;
    for (int i = 0; i < S.length(); i++){
        if (S.substr(i, T.length()) == T){
            Q.push_back(S.substr(pre, i - pre));
            pre = i + T.length();
        }
    }
    return Q;
}
bool cmp(const node &a, const node &b)
{
    if (a.author < b.author) return 1;
    if (a.author > b.author) return 0;
    if (a.title < b.title) return 1;
    if (a.title > b.title) return 0;
    return a.volume < b.volume;
}
int main()
{
    //ios::sync_with_stdio(false);
    freopen("bibtex.in", "r", stdin);
    freopen("bibtex.out", "w", stdout);
    while(cin >> head){
        cin >> col; //"{"
        node now;
        now.volume = 0;
        now.number = 0;
        now.pages.clear();
        if (head == "@book") now.type = 2;
        if (head == "@article") now.type = 1;
        while(cin >> str && str != "}"){
            if (str == ",") continue;
            cin >> col; // "="
            char ch;
            while (1) {
                ch = getchar();
                if (ch == '"') break;
            }
            char st[2000];
            scanf("%[^\"]", st); getchar();
            //getline(cin, line);
            line = (string)st;
            //cout << line << endl;
            if (str == "author"){
                //and
                vector<string> temp = split(line, " and ");
                now.author.clear();
                for (int i = 0; i < temp.size(); i++){
                    now.author.push_back(split(temp[i], " "));
                    vector<string> &vs = now.author.back();
                    rotate(vs.begin(), vs.begin() + vs.size() - 1, vs.end());
                }
                sort(now.author.begin(), now.author.end());
            }
            if (str == "title"){
                now.title = line;
            }
            if (str == "journal" || str == "publisher"){
                // a & ' ' & 1 & -
                now.journal = line;
            }
            if (str == "year"){
                now.year = get(line);
            }
            if (str == "volume"){
                now.volume = get(line);
            }
            if (str == "number"){
                now.number = get(line);
            }
            if (str == "pages"){
                now.pages = split(line, "--");
            }
        }
        Q.push_back(now);
    }
    sort(Q.begin(), Q.end(), cmp);
    for (int i = 0; i < Q.size(); i++){
        //printf("%d\n", Q[i].volume);
        printf("[%d] ", i + 1);
        for (int j = 0; j < Q[i].author.size(); j++){
            if (j != 0) printf(", ");
            vector<string> &ss = Q[i].author[j];
            printf("%s", ss[0].c_str());
            for (int k = 1; k < ss.size(); k++){
                printf(" %c.", ss[k][0]);
            }
        }
        printf(" %s", Q[i].title.c_str());
        if (Q[i].type == 1){
            printf(" // %s", Q[i].journal.c_str());
            if (Q[i].volume != 0){
                printf(", %d", Q[i].volume);
            }
            if (Q[i].number != 0){
                printf(" (%d)", Q[i].number);
            }
            printf(" -- %d", Q[i].year);
            if (Q[i].pages.size() == 1){
                printf(" -- p. %s", Q[i].pages[0].c_str());
            }
            if (Q[i].pages.size() == 2){
                printf(" -- pp. %s--%s", Q[i].pages[0].c_str(), Q[i].pages[1].c_str());
            }
            puts("");
        }
        if (Q[i].type == 2){    
            if (Q[i].volume != 0){
                printf(", Vol. %d", Q[i].volume);
            }
            printf(" -- %s, %d", Q[i].journal.c_str(), Q[i].year);
            puts("");
        }
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}