当前位置:网站首页>华为面试题: 没有回文串
华为面试题: 没有回文串
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;
}
边栏推荐
- 什么是 eRDMA?丨科普漫画图解
- Solving the longest subsequence with linear DP -- three questions
- Fabric. JS manual bold text iText
- < 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
- STM32 standard firmware library function name (I)
- 1. Editing weapon VIM
- 报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
- [QNX Hypervisor 2.2用户手册]6.3 Guest与外部之间通信
- Talk about idempotent design
- C语言高级用法--函数指针:回调函数;转换表
猜你喜欢
微信小程序使用towxml显示公式
Simple verification code generator for 51 single chip microcomputer experiment
MathML to latex
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
< 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
JMeter script parameterization
A white hole formed by antineutrons produced by particle accelerators
QT new project
报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
How kaggle uses utility script
随机推荐
【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境
Basic knowledge of QT original code
Error: NPM warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
NLA自然语言分析实现数据分析零门槛
threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
卷积神经网络(入门)
buuctf-pwn write-ups (7)
Yolov6 training: various problems encountered in training your dataset
Wechat applet uses towxml to display formula
Chapter 9: xshell free version installation
2、const 型指针
检查密码
富文本编辑器添加矢量公式(MathType for TinyMCE ,可视化添加)
字符串匹配问题
测试框架TestNG的使用(二):testNG xml的使用
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
Method of creating linked server for cross server data access
Simple verification code generator for 51 single chip microcomputer experiment
Fabric.js 自由绘制椭圆
kityformula-editor 配置字号和间距