当前位置:网站首页>华为面试题: 没有回文串
华为面试题: 没有回文串
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;
}
边栏推荐
- 2. Const pointer
- STM32 standard firmware library function name memory (II)
- 3. Function pointers and pointer functions
- 一张图彻底掌握prototype、__proto__、constructor之前的关系(JS原型、原型链)
- 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
- taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
- 4. Array pointer and pointer array
- 用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
- taobao. trade. Get (get some information of a single transaction), Taobao store order interface, Taobao oauth2.0 interface, Taobao R2 interface code docking and sharing
- buuctf-pwn write-ups (7)
猜你喜欢

HMS core machine learning service helps zaful users to shop conveniently

跨服务器数据访问的创建链接服务器方法

fatal: unsafe repository is owned by someone else 的解决方法

Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022

STM32-DAC实验&高频DAC输出测试

How kaggle uses utility script

Yolov3 & yolov5 output result description

QT new project
![[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment](/img/e1/c8e81570ab78de1e488a611c25ebb9.png)
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment

Large top heap, small top heap and heap sequencing
随机推荐
mongodb的认识
Socket and socket address
Tencent cloud tstor unified storage passed the evaluation of the first batch of basic file storage capabilities of the ICT Institute
buuctf-pwn write-ups (7)
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
数据库连接池和数据源
Solving the longest subsequence with linear DP -- three questions
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Simple verification code generator for 51 single chip microcomputer experiment
Quick analysis: easy to share the Internet
Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment
Stm32-dac Experiment & high frequency DAC output test
STM32 library function for GPIO initialization
Fabric.js 自由绘制椭圆
STM32 standard firmware library function name (I)
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
Uniapp automated test learning
< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
Implement a server with multi process concurrency