当前位置:网站首页>华为面试题: 没有回文串
华为面试题: 没有回文串
2022-07-02 11:26:00 【四库全书的酷】
题目
【没有回文串】
回文串的定义:正读和反读都一样的字符串
现在已经存在一个不包含回文串的字符串,字符串的字符都是在英语字母的前N个,且字符串不包含任何长度大于等于2的回文串;请找出下一个字典序的不包含回文串的、字符都是在英语字母的前N个、且长度相同的字符串。如果不存在,请输出NO。
输入描述:
第一行有一个整数:N(1<=N<=26),表示字符串的每个字符范围都是前N的英语字母。
第二行输入一个字符串(输入长度<=10000),输入保证这个字符串是合法的并且没有包含回文串。
输出描述:
输出下一个字典序的不包含回文串的、字符都是在英语字母的前N个、且长度相同的字符串;如果不存在,请输出”NO”。
示例1:
输入:
3
cba
输出:
NO
示例2:
输入:
4
dbad
输出:
dbcd
代码
#include<iostream>
using namespace std;
int main(){
int n;
cin >> n;
string str;
cin >> str;
int len = str.size();
if(len == 1) {
cout << "NO" << endl;
return 0;
}
for(int i = len - 1; i >= 0; --i){
char next = str[i] + 1;
while(next < 'a'+ n){
int flag = 0;
for(int j = i - 2; j <= i + 2; ++j){
if(j < 0) continue;
if(j >= len) continue;
if(j == i) continue;
if(str[j] == next){
flag = 1;
break;
}
}
if(flag){
next = next + 1;
}else{
str[i] = next;
cout << str << endl;
return 0;
}
}
}
cout << "NO" << endl;
return 0;
}
边栏推荐
- Ad20 cannot select the solution of component packaging in PCB editor
- taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
- 1、编辑利器vim
- [apipost] tutorial
- Fabric. JS zoom canvas
- MathML to latex
- Li Chuang EDA learning notes 15: draw border or import border (DXF file)
- Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
- Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
- 什么是 eRDMA?丨科普漫画图解
猜你喜欢

没有从远程服务器‘‘映射到本地用户‘(null)/sa‘的远程用户‘sa‘及服务主密码解密错误的解决办法

Fabric. JS zoom canvas

《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持

Fabric.js 橡皮擦的用法(包含恢复功能)

Wechat applet uses towxml to display formula
[email protected]: The platform “win32“ is incompatible with this module."/>info [email protected]: The platform “win32“ is incompatible with this module.

The use of TestNG, the testing framework (II): the use of TestNG XML

<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持

Fabric.js 自由绘制圆形

There is no solution to the decryption error of the remote user 'sa' and the service master password mapped from the remote server 'to the local user' (null) /sa '
随机推荐
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
测试框架TestNG的使用(二):testNG xml的使用
检查密码
docker mysql
Solving the longest subsequence with linear DP -- three questions
taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
mongodb的认识
Yolov6 training: various problems encountered in training your dataset
2. Const pointer
3. Function pointers and pointer functions
HMS core machine learning service helps zaful users to shop conveniently
Talk about idempotent design
NLA natural language analysis makes data analysis more intelligent
Check password
Stm32-dac Experiment & high frequency DAC output test
uniapp自动化测试学习
【apipost】使用教程
求轮廓最大内接圆
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口