当前位置:网站首页>华为面试题: 没有回文串
华为面试题: 没有回文串
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;
}
边栏推荐
- Fabric. Usage of JS eraser (including recovery function)
- 途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
- <口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
- Daily learning 3
- Fundamentals of software testing
- 天猫商品详情接口(APP,H5端)
- Use of freemaker
- The use of TestNG, the testing framework (II): the use of TestNG XML
- Pychart connects to the remote server
- Generally speaking, if the error of inconsistent tab and space occurs frequently
猜你喜欢

Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)

Teamtalk source code analysis win client

Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.

MQ tutorial | exchange (switch)

Fundamentals of software testing

Add vector formula in rich text editor (MathType for TinyMCE, visual addition)

途家木鸟美团夏日折扣对垒,门槛低就一定香吗?

Method of creating linked server for cross server data access

Socket and socket address

Design and implementation of car query system based on php+mysql
随机推荐
Full of knowledge points, how to use JMeter to generate encrypted data and write it to the database? Don't collect it quickly
Tujia muniao meituan has a discount match in summer. Will it be fragrant if the threshold is low?
taobao. logistics. dummy. Send (no logistics delivery processing) interface, Taobao store delivery API interface, Taobao order delivery interface, Taobao R2 interface, Taobao oau2.0 interface
【apipost】使用教程
大顶堆、小顶堆与堆排序
PTA题库 ===>复数四则运算,一帮一,考试座位号(7-73)
报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
threejs的控制器 立方體空間 基本控制器+慣性控制+飛行控制
PTA question bank== > complex four operations, one for one, examination seat number (7-73)
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
由粒子加速器产生的反中子形成的白洞
删除元素(带过渡动画)
跨服务器数据访问的创建链接服务器方法
taobao.trade.memo.add( 对一笔交易添加备注 )接口,淘宝店铺插旗接口,淘宝订单插旗API接口,oAuth2.0接口
STM32 standard firmware library function name (I)
Daily learning 3
HMS core machine learning service helps zaful users to shop conveniently
1、编辑利器vim
fatal: unsafe repository is owned by someone else 的解决方法
Fabric. JS zoom canvas