当前位置:网站首页>华为面试题: 没有回文串
华为面试题: 没有回文串
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;
}
边栏推荐
- Design and implementation of car query system based on php+mysql
- STM32 standard firmware library function name memory (II)
- It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
- Obsidian installs third-party plug-ins - unable to load plug-ins
- 使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
- Makefile 分隔文件名与后缀
- taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
- Factal: Unsafe repository is owned by someone else Solution
- 富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
- [QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
猜你喜欢
##51单片机实验之简易验证码发生器
php链表创建和遍历
Obsidian installs third-party plug-ins - unable to load plug-ins
大顶堆、小顶堆与堆排序
Li Chuang EDA learning notes 15: draw border or import border (DXF file)
STM32 library function for GPIO initialization
Stm32-dac Experiment & high frequency DAC output test
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
Yolov3 & yolov5 output result description
Ad20 cannot select the solution of component packaging in PCB editor
随机推荐
Tmall product details interface (APP, H5 end)
Fabric.js 手动加粗文本iText
Makefile separates file names and suffixes
Use of freemaker
删除元素(带过渡动画)
《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
< schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
Fabric. Keep the original level when JS element is selected
复用和分用
Fatal: unsafe repository is owned by someone else
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
PTA question bank== > complex four operations, one for one, examination seat number (7-73)
Xilinx Vivado set *. svh as SystemVerilog Header
测试框架TestNG的使用(二):testNG xml的使用
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
MQ tutorial | exchange (switch)
HMS core machine learning service helps zaful users to shop conveniently
buuctf-pwn write-ups (7)
kityformula-editor 配置字号和间距