【UVA】230 - Borrowers(map模拟)

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

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

14072706 230 Borrowers Accepted C++ 0.015 2014-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;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值