其他分享
首页 > 其他分享> > CSP-22-3 DHCP服务器

CSP-22-3 DHCP服务器

作者:互联网

模拟题

原题链接: https://www.acwing.com/problem/content/3416/

分析题意,注意细节。

可以不用STL,如果能自己模拟,就不要用STL。

#include <bits/stdc++.h>
using namespace std;

/*
1. 客户端 --Discover--> 服务器 请求IP地址
2. 服务器 --Offer-->    客户端 分配空闲的IP地址
3. 客户端选择一个IP --Request--> 服务器
4. 服务器分析:
	4.1 选择我发送的报文:再次确认
		4.1.1 服务器 --ACK--> 客户端 确认配置有效
		4.1.2 服务器 --NAK--> 客户端 地址无效
	4.2 没选择我的:解除我设置的占用
5.1 客户端收到ACK,完成网络配置(有效时间)
5.2 客户端收到NAK,回到步骤1.
6. 有效时间结束前延时。
	服务器按照步骤4延长(广播,但是只有分配服务器应答)
	客户端按照步骤5处理。

服务器的参数:
1. 地址池大小N,能分配的IP为1~N
2. 默认过期时间Tdef
3. 过期时间的上限和下限Tmax、Tmin
4. 本机名称 H

*/

const int MAXN = 10010;

int N, Tdef, Tmax, Tmin;
string H;
int n;
struct Package {
	string send;
	string accept;
	string type;
	int IP;
	int late;
};
// 地址池  0表示未占用 1表示待分配 2表示占用 3表示过期
int IP_pool[MAXN];
// IP 占用者列表
string IP_owner[MAXN];
// 对应IP的过期时间
int IP_late[MAXN];

int main() {
	cin >> N >> Tdef >> Tmax >> Tmin >> H;
	cin >> n;
	for (int ct = 1; ct <= n; ct++) {
		// 接收时间和报文内容
		int ti;
		Package pack;
		cin >> ti >> pack.send >> pack.accept >>
		    pack.type >> pack.IP >> pack.late;

		// 更新IP状态
		for (int i = 1; i <= N; i++) {
			if (IP_pool[i] == 1) {
				if (IP_late[i] <= ti) {
					IP_late[i] = 0;
					IP_pool[i] = 0;
					IP_owner[i] = "";
				}
			}
			if (IP_pool[i] == 2) {
				if (IP_late[i] <= ti) {
					IP_late[i] = 0;
					IP_pool[i] = 3;
				}
			}
		}

		//不处理情况:
		if (pack.accept != H && pack.accept != "*") {
			if (pack.type != "REQ")
				continue;
		}
		if (pack.type != "DIS" && pack.type != "REQ") {
			continue;
		}
		if ((pack.accept == "*" && pack.type != "DIS") ||
		        (pack.accept == H && pack.type == "DIS")) {
			continue;
		}

		if (pack.type == "DIS") {
			int flag = 0;
			int min_0 = 0;
			int min_3 = 0;
			Package offer;
			offer.accept = pack.send;
			offer.send = H;
			offer.type = "OFR";
			for (int i = 1; i <= N; i++) {
				if (flag == 0 && IP_owner[i] == pack.send) {
					// 分配IP
					flag = i;
				}
				if (min_0 == 0 && IP_pool[i] == 0) {
					min_0 = i;
				}
				if (min_3 == 0 && IP_pool[i] == 3) {
					min_3 = i;
				}
			}

//			cout << "IP_1: " << IP_pool[1] << endl;
//			cout << "flag: " << flag << endl;
//			cout << "min_0: " << min_0 << endl;
//			cout << "min_3: " << min_3 << endl;

			if (flag != 0) {
				offer.IP = flag;
				// 处理分配后的IP池
				IP_pool[flag] = 1;
				IP_owner[flag] = pack.send;
				if (pack.late == 0) {
					offer.late = ti + Tdef;
				} else {
					int late_time = pack.late - ti;
					if (late_time < Tmin) {
						offer.late = ti + Tmin;
					} else if (late_time > Tmax) {
						offer.late = ti + Tmax;
					} else {
						offer.late = pack.late;
					}
				}
				IP_late[flag] = offer.late;
			} else {
				if (min_0 != 0) {
					offer.IP = min_0;
					// 处理分配后的IP池
					IP_pool[min_0] = 1;
					IP_owner[min_0] = pack.send;
					//cout << IP_owner[1] << endl;
					if (pack.late == 0) {
						offer.late = ti + Tdef;
					} else {
						int late_time = pack.late - ti;
						if (late_time < Tmin) {
							offer.late = ti + Tmin;
						} else if (late_time > Tmax) {
							offer.late = ti + Tmax;
						} else {
							offer.late = pack.late;
						}
					}
					IP_late[min_0] = offer.late;
				} else if (min_3 != 0) {
					offer.IP = min_3;
					// 处理分配后的IP池
					IP_pool[min_3] = 1;
					IP_owner[min_3] = pack.send;
					if (pack.late == 0) {
						offer.late = ti + Tdef;
					} else {
						int late_time = pack.late - ti;
						if (late_time < Tmin) {
							offer.late = ti + Tmin;
						} else if (late_time > Tmax) {
							offer.late = ti + Tmax;
						} else {
							offer.late = pack.late;
						}
					}
					IP_late[min_3] = offer.late;
				} else
					continue;
			}

			// 发送Offer报文
			cout << offer.send << " " << offer.accept << " "
			     << offer.type << " " << offer.IP << " " <<
			     offer.late << endl;
		} else if (pack.type == "REQ") {

			Package offer;
			offer.send = H;
			offer.accept = pack.send;
			if (pack.accept != H) {
				for (int i = 1; i <= N; i++) {
					if (IP_owner[i] == pack.send && IP_pool[i] == 1) {
						IP_owner[i] = "";
						IP_pool[i] = 0;
						// 清零过期时刻?
						IP_late[i] = 0;
					}
				}
				continue;
			}


			if (pack.IP > N || IP_owner[pack.IP] != pack.send) {

				offer.type = "NAK";
				offer.IP = pack.IP;
				offer.late = 0;

				cout << offer.send << " " << offer.accept << " "
				     << offer.type << " " << offer.IP << " " <<
				     offer.late << endl;

				continue;
			}

			IP_pool[pack.IP] = 2;
			offer.IP = pack.IP;
			if (pack.late == 0) {
				offer.late = ti + Tdef;
			} else {
				int late_time = pack.late - ti;
				if (late_time < Tmin) {
					offer.late = ti + Tmin;
				} else if (late_time > Tmax) {
					offer.late = ti + Tmax;
				} else {
					offer.late = pack.late;
				}
			}
			IP_late[pack.IP] = offer.late;

			offer.type = "ACK";
			cout << offer.send << " " << offer.accept << " "
			     << offer.type << " " << offer.IP << " " <<
			     offer.late << endl;
		}

	}

	return 0;
}

标签:22,offer,int,IP,Tmax,late,DHCP,CSP,pack
来源: https://www.cnblogs.com/superPG/p/16341665.html