CodeForces 1976A Verify Password

题目

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

East_17

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值