我天呢经历了带配额的文件系统我真的极其挫败,然后开始写的时候觉得这个题感觉怎么有点简单...然后就觉得肯定是自己有很多东西没有想到,而且写的时候破罐子破摔觉得肯定会超时...
结果一写!哦买噶居然满分了!
脑子不清醒的时候写的差不多也就两个小时不到吧,天哪第三题要是是这种难度也太幸福了orz
cpp
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 +10;
string H;
ll M, Tdef, Tmax, Tmin;
map<string, set<ll> > user_ip;
struct Ip{ //定义一个IP结构体
int status = 1; //1表示未分配,2表示待分配,3表示占用,4表示过期
string user = "";
ll ddl = 0;
};
Ip ipPool[N]; //定义一个IP池
bool compare(string a, string b)
{
if(a.size() != b.size()) return false;
for(int i = 0;i < a.size();i ++){
if(a[i] != b[i])
return false;
}
return true;
}
//在每个时刻前进行检查
void check(ll t)
{
for(ll i = 1;i <= M;i ++){
if(ipPool[i].ddl && t >= ipPool[i].ddl){
if(ipPool[i].status == 2){ //待分配状态
ipPool[i].status = 1;
string user = ipPool[i].user;
ipPool[i].user = "";
ipPool[i].ddl = 0;
user_ip[user].erase(i);
if(!user_ip[user].size())
user_ip.erase(user);
}
else if(ipPool[i].status == 3){ //占用变为过期
ipPool[i].status = 4;
ipPool[i].ddl = 0;
}
}
}
}
void Discover(const ll t,const string from,const string to,const ll ddl)
{
ll id; bool flag = false;
if(to != "*") return;
if(user_ip.count(from)){
id = *user_ip[from].begin(); //取出第一个
flag = true;
}
else {
for(ll i = 1;i <= M ;i ++){
if(ipPool[i].status == 1){
id = i;
flag = true;
break;
}
}
for(ll i = 1;i <= M && !flag;i ++){
if(ipPool[i].status == 4){
id = i;
string user = ipPool[i].user;
user_ip[user].erase(i);
if(!user_ip[user].size()) user_ip.erase(user);
flag = true;
break;
}
}
}
if(!flag) return;
ipPool[id].status = 2;
ipPool[id].user = from;
user_ip[from].insert(id);
ll overtime;
if(!ddl) overtime = t + Tdef;
else{
if(ddl - t >= Tmin && ddl - t <= Tmax) overtime = ddl;
else if(ddl - t > Tmax) overtime = t + Tmax;
else overtime = t + Tmin;
}
ipPool[id].ddl = overtime;
cout << H <<' '<<from<<' '<<"OFR "<<id<<' '<<overtime<<"\n";
}
void Request(const ll t,const string from,const string to,const ll ip,const ll ddl)
{
if(to == "*") return;
if(!compare(to, H)){
auto temp = user_ip[from];
for(auto x : temp){
if(ipPool[x].status == 2){
ipPool[x].status = 1;
ipPool[x].user = "";
ipPool[x].ddl = 0;
user_ip[from].erase(x);
}
}
if(!user_ip[from].size()) user_ip.erase(from);
return;
}
if(ip >= 1 && ip <= M && compare(ipPool[ip].user, from)){
ipPool[ip].status = 3;
ll overtime;
if(!ddl) overtime = t + Tdef;
else{
if(ddl - t >= Tmin && ddl - t <= Tmax) overtime = ddl;
else if(ddl - t > Tmax) overtime = t + Tmax;
else overtime = t + Tmin;
}
ipPool[ip].ddl = overtime;
cout << H <<' '<<from<<' '<<"ACK "<<ip<<' '<<overtime<<"\n";
}else{ //发送Nak报文
cout << H <<' '<<from<<' '<<"NAK "<<ip<<' '<<0<<"\n";
}
}
int main()
{
ll n;
cin >> M >> Tdef >> Tmax >> Tmin >> H;
cin >> n;
for(ll i = 1;i <= M;i ++){
ipPool[i].status = 1;
ipPool[i].ddl = 0;
ipPool[i].user = "";
}
while(n --){
ll t, ip ,ddl;
string from, to , type;
cin >> t >> from >> to >> type >> ip >> ddl;
check(t);
if(type == "DIS"){
Discover(t,from,to,ddl);
}else if(type == "REQ"){
Request(t, from, to, ip, ddl);
}
}
return 0;
}
当你感觉第三题写不下去了就来写DHCP服务器吧!!