Skip to content
Author: loop3r
Date: 20260223
tag: 模拟
link: https://www.luogu.com.cn/problem/P2815

问题描述

link

分析

按题意模拟。

参考代码

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

int main() {
	string s; cin >> s;

	vector<string> v;
	string t = "";
	for (int i = 0; i < s.size(); i++) {
		if (s[i] == ':') {
			v.push_back(t);
			t = "";
		} else {
			t += s[i];
		}
	}
	v.push_back(t);

	int l = -1, r = -1, mx = 0;
	for (int i = 0; i < v.size(); i++) {
		if (v[i] == "0000") {
			int j = i + 1;
			while (j < v.size() && v[j] == "0000") j++;
			if (j - i > mx) {
				l = i;
				r = j - 1;
				mx = j - i;
			}
		}
	}

	for (int i = 0; i < v.size(); i++) {
		if (i < l) {
			string t = v[i];
			int j = 0;
			while (j < t.size() - 1 && t[j] == '0') j++;
			while (j < t.size()) {
				cout << t[j];
				j++;
			}
			if (i < l - 1) cout << ":";
		}
		if (i == l) cout << ":";
		if (i == r) cout << ":";
		if (i > r) {
			string t = v[i];
			int j = 0;
			while (j < t.size() - 1 && t[j] == '0') j++;
			while (j < t.size()) {
				cout << t[j];
				j++;
			}
			if (i < v.size() - 1) cout << ":";
		}
	}

    return 0;
}