题目
A. Verify Password
Monocarp is working on his new site, and the current challenge is to make the users pick strong passwords.
Monocarp decided that strong passwords should satisfy the following conditions:
- password should consist only of lowercase Latin letters and digits;
- there should be no digit that comes after a letter (so, after each letter, there is either another letter or the string ends);
- all digits should be sorted in the non-decreasing order;
- all letters should be sorted in the non-decreasing order.
Note that it's allowed for the password to have only letters or only digits.
Monocarp managed to implement the first condition, but he struggles with the remaining ones. Can you help him to verify the passwords?
Input
The first line contains a single integer t𝑡 (1≤t≤10001≤𝑡≤1000) — the number of testcases.
The first line of each testcase contains a single integer n𝑛 (1≤n≤201≤𝑛≤20) — the length of the password.
The second line contains a string, consisting of exactly n𝑛 characters. Each character is either a lowercase Latin letter or a digit.
Output
For each testcase, print "YES" if the given password is strong and "NO" otherwise.
题目描述
Monocarp正在开发他的新网站,目前的挑战是让用户选择强密码。
Monocarp认为强密码应该满足以下条件:
密码只能由小写拉丁字母和数字组成;
字母后面不能有数字(因此,在每个字母后面,要么有另一个字母,要么字符串结束);
所有数字应按非降序排序;
所有字母应按非降序排序。
请注意,密码只允许包含字母或数字。
Monocarp成功地实现了第一个条件,但他在其余的条件下挣扎。你能帮他验证一下密码吗?
输入
第一行包含一个整数t(1≤t≤1000)——测试用例的数量。
每个测试用例的第一行包含一个整数n(1≤n≤20) -密码的长度。
第二行包含一个字符串,由n个字符组成。每个字符是一个小写拉丁字母或一个数字。
输出
对于每个测试用例,如果给定的密码是强密码,则打印“YES”,否则打印“NO”。
样例
输入
5
4
12ac
5
123wa
9
allllmost
5
ac123
6
011679
输出
YES NO YES NO YES
答案
#include<iostream>
#include<cstring>
using namespace std;
bool Is_Digit(char s)
{
return s>='0'&&s<='9';
}
bool Is_Letter(char s)
{
return s>='a'&&s<='z';
}
bool PassWord(string s,int n)
{
for(int i = 0; i < n - 1; i++)
{
if(Is_Digit(s[i]) && Is_Digit(s[i+1]))
{
if(s[i]>s[i+1])
return false;
}
if(Is_Letter(s[i]) && Is_Letter(s[i+1]))
{
if(s[i]>s[i+1])
return false;
}
if(Is_Letter(s[i]) && Is_Digit(s[i+1]))
return false;
}
return true;
}
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
string s;
cin >> s;
if(PassWord(s, n)) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}