码迷,mamicode.com
首页 > 其他好文 > 详细

【UVA】230 - Borrowers(map模拟)

时间:2014-08-21 11:31:04      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   ar   

利用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;
}


【UVA】230 - Borrowers(map模拟),布布扣,bubuko.com

【UVA】230 - Borrowers(map模拟)

标签:style   blog   http   color   os   io   for   ar   

原文地址:http://blog.csdn.net/u013451221/article/details/38726941

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!