首页 > 代码库 > 【UVA】230 - Borrowers(map模拟)

【UVA】230 - Borrowers(map模拟)

利用map<string,int>判断一本书的状态,0代表借出去了,1代表在书架,2代表借出去换回来但是还未放回书架

设计一些字符串的处理问题,用一些字符串搜索函数比如 strstr , strchar等等

14072706230BorrowersAcceptedC++0.0152014-08-21 02:59:27

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<string>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define INF (1 << 10)
#define esp 1e-9
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================
===========================================*/
#define MAXD 1000 + 10
#define LEN 80 + 10
struct Book{
    char name[LEN];
    char author[LEN];
    friend bool operator < (Book p , Book q){
        int e1 = strcmp(p.author,q.author);
        int e2 = strcmp(p.name,q.name);
        if(e1 != 0){
            if(e1 > 0)
                return false;
            else
                return true;
        }
        else{
            if(e2 > 0)
                return false;
            else
                return true;
        }
    }
}book[MAXD];
int n = 0;
map<string,int>value;
int find_pre(int cur){
    for(int i = cur - 1 ; i >= 0 ; i--){
         if(value[book[i].name] == 1)
            return i;
    }
    return -1;
}
int main(){
    char str[LEN];
    int  pos;
    value.clear();
    while(gets(str)){
        if(strcmp(str,"END") == 0)
            break;
        pos = strchr(str + 1, '"') - str;
        strncpy(book[n].name,str,pos + 1);
        book[n].name[pos + 2] = '\0';
        pos = strstr(str + pos , "by") - str;
        strcpy(book[n].author,str + pos + 3);
        value[book[n].name] = 1;
        n++;
    }
    sort(book , book + n);
    while(scanf("%s",str)){
        if(strcmp(str,"END") == 0)
            break;
        if(strcmp(str,"BORROW") == 0){
             gets(str);
             value[str + 1] = 0;
        }
        else if(strcmp(str,"RETURN") == 0){
             gets(str);
             value[str + 1] = 2;
        }
        else if(strcmp(str,"SHELVE") == 0){
             for(int i = 0 ; i < n ; i++)
                if(value[book[i].name] == 2){
                      pos = find_pre(i);
                      if(pos == -1)
                        printf("Put %s first\n",book[i].name);
                      else
                        printf("Put %s after %s\n",book[i].name,book[pos].name);
                      value[book[i].name] = 1;
                }
             printf("END\n");
        }
    }
    return 0;
}