Skip to content

Instantly share code, notes, and snippets.

@Cycatz
Last active April 6, 2017 05:06
Show Gist options
  • Save Cycatz/795a04232f0cac7130fab42ca6261e2a to your computer and use it in GitHub Desktop.
Save Cycatz/795a04232f0cac7130fab42ca6261e2a to your computer and use it in GitHub Desktop.
[UVa]230 - Borrowers
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<cstdio>
using namespace std;
typedef pair<string, string> PSS;
set<PSS> lis, temp;
map<string, string> link;
void printcode(set<PSS> x) // debug
{
for(set<PSS> :: iterator it = x.begin() ; it!= x.end() ; it++)
cout << it->first << it -> second << endl ;
}
void div(string s, string &N, string &A)
{
int len = s.length(), idx = 0, r;
for (int i = 0; i < len; i++)
{
if (s[i] == '"')idx++;
N += s[i];
if (idx == 2) { r = i; break; }
}
A = s.substr(r + 5, len - (r + 5) + 1);
}
int main()
{
//freopen("data.txt" , "w" , stdout);
string bookP, cmd, name, arthor;
while (getline(cin, bookP) && bookP != "END")
{
arthor = name = "";
div(bookP, name, arthor);
lis.insert(make_pair(arthor, name));
link[name] = arthor;
}
while (cin >> cmd && cmd != "END")
{
if (cmd[0] == 'B')
{
getchar(); getline(cin, name); // because of the blank
lis.erase(make_pair(link[name], name));
}
else if (cmd[0] == 'R')
{
getchar(); getline(cin, name); // because of the blank
temp.insert(make_pair(link[name], name)); // The place of returning books
}
else if (cmd[0] == 'S')
{
set<PSS> ::iterator it1, it2;
for (auto it = temp.begin(); it != temp.end(); it++)
{
PSS book = *it;
auto insert_value = lis.insert(book);
it1 = insert_value.first;
if (it1 == lis.begin())
cout << "Put " << it1->second << " first" << endl;
else
{
it2 = it1; it2--; //no match for 'operator-' , so I just do this XD
cout << "Put " << it1->second << " after " << it2->second << endl;
}
}
cout << "END" << endl;
temp.clear();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment