思路:

  1. 遍历输入的每个字符串,判断是否为“漂亮串”。
  2. 对于每个字符串,使用双指针法判断是否满足条件。
  3. 如果满足条件,输出"YES",否则输出"NO"。

优化:

  1. 不需要使用字符串s存储输入的每个字符串,直接使用cin读取即可。
  2. 不需要使用dnm标记是否满足条件,直接根据指针位置判断即可。

改进后的代码如下:

#include using namespace std;

int main() { int n; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; int len = s.size(); int i = 0, j = len - 1; bool isBeautiful = true; while (i <= j) { if (s[i] == s[j] + 1) { i++; } else if (s[i] + 1 == s[j]) { j--; } else { isBeautiful = false; break; } } if (isBeautiful) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0;

#include bitsstdc++husing namespace std;string s;int main 	int n dnm = 0;	cin n;	for int sb = 0; sb n; sb++ 		cin s;		int len = ssize;		for int i = 0 ii = len; ii len; 			if si == sii + 1 				i++;

原文地址: https://www.cveoy.top/t/topic/h6t8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录