文章

124

粉丝

0

获赞

137

访问

8.8k

头像
字符串是否合法 题解:
P5260 浙江工商大学2023年机试题
发布于2026年2月5日 09:34
阅读数 49

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

int main(){
	int n;
	cin >> n;
	string str;
	for(int i=0;i<n;i++){
		cin >> str;
		int len = str.length();
		int tag = 1;
		for(int j=0;j<len-1;j++){
			if(str[j] >= 'a' && str[j] <= 'z'){
				if(str[j+1] ==  toupper(str[j]) || str[j+1] == 'a'+(str[j]-'a'+2)%26){
					tag = 1;
				}		
				else{
					tag = 0;
					break;
				}	
			}	
			else if(str[j] >= 'A' && str[j] <= 'Z'){
				if(str[j+1] ==  tolower(str[j]) || str[j+1] == 'A'+(str[j]-'A'-2+26)%26 ){ //+26避免负数!!!
					tag = 1;
				}	
				else{
					tag = 0;
					break;
				}	
			}				
		}
		if(tag == 1)
			cout << "Y" << endl;
		else
			cout << "N" << endl;
	}	
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发