跳到正文

题解

题解:P12005 【MX-X10-T1】[LSOT-4] 如何用中间忘了写一篇排版整齐的题解?

yyycj约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
P12005

版本

  1. v1当前

题目简述

给你一段字符串,只包括 a@1$,_ 中的字符,要求不能出现相邻的 @ 和 a1$ 中的任意一个字符,也不能出现 , 与 a1$ 中的任意一个字符存在 一个或多个 _。如果违反了以上规定,输出 No,反之输出 Yes。

主要思路

十分明显的%你题,第一条规定可以直接在遍历到 @ 是判断是否有 a1$ 中的任意一个字符相邻就行了;

第二条规定可以在遍历到 , 时,如果左边是 _,那么向左一直判断是否存在 a1$ 中的任意一个字符,直到到达边界或再遇到 _,右边同理。

AC Code

C++
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int INT_INF = 0x3f3f3f3f;
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
template<typename T1, typename T2, typename T3> T1 _pow(T1 a, T2 b, T3 mod = LL_INF) { T1 x = 1, y = a; while(b > 0) {if (b & 1) x = x * y % mod; y = y * y % mod; b >>= 1; } return x; }
// ----------------------------

// ----------------------------

// ----------------------------


int main() {
	string s; cin >> s;
	// ----------------------------
	int idx;
	bool flag = true;
	int n = s.length();
	for (int i = 0; i < n - 1; i++) {
		if (s[i] == '@') {
			if (i > 0 && (s[i - 1] == 'a' || s[i - 1] == '1' || s[i - 1] == '$') ||
				s[i + 1] == 'a' || s[i + 1] == '1' || s[i + 1] == '$') flag = false;
		}
		else if (s[i] == ',') {
			if (i > 0 && s[i - 1] == '_') {
				idx = i - 2;
				while (idx >= 0) {
					if (s[idx] == 'a' || s[idx] == '1' || s[idx] == '$') flag = false;
					if (s[idx] != '_') break;
					idx--;
				}
			}
			if (s[i + 1] == '_') {
				idx = i + 2;
				while (idx < (int)s.length()) {
					if (s[idx] == 'a' || s[idx] == '1' || s[idx] == '$') flag = false;
					if (s[idx] != '_') break;
					idx++;
				}
			}
		}
	}
	// ----------------------------
	if (flag) cout << "Yes";
	else cout << "No";
	return 0;
}
原文