当前位置:网站首页>华为面试题: 没有回文串
华为面试题: 没有回文串
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;
}
边栏推荐
- Quick analysis: easy to share the Internet
- 字符串匹配问题
- STM32标准固件库函数名记忆(二)
- [apipost] tutorial
- STM32 standard firmware library function name memory (II)
- Simple verification code generator for 51 single chip microcomputer experiment
- 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
- Use of freemaker
- PyQt5_ Qscrollarea content is saved as a picture
- NLA自然语言分析实现数据分析零门槛
猜你喜欢

Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)

Actual combat sharing of shutter screen acquisition

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

Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people

Onnx+tensorrt: write preprocessing operations to onnx and complete TRT deployment

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 '

天猫商品详情接口(APP,H5端)

STM32库函数进行GPIO初始化

The most complete analysis of Flink frame window function

使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式
随机推荐
检查密码
Fabric. JS free drawing ellipse
mongodb的认识
YoloV6训练:训练自己数据集遇到的各种问题
Tmall product details interface (APP, H5 end)
PyQt5_ Qscrollarea content is saved as a picture
大顶堆、小顶堆与堆排序
Why can't browsers read JSX?
ONNX+TensorRT:将预处理操作写入ONNX并完成TRT部署
fatal: unsafe repository is owned by someone else 的解决方法
NLA natural language analysis realizes zero threshold of data analysis
threejs的控制器 立方体空间 基本控制器+惯性控制+飞行控制
STM32库函数进行GPIO初始化
php链表创建和遍历
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
mathML转latex
C#代码审计实战+前置知识
Thoroughly master prototype__ proto__、 Relationship before constructor (JS prototype, prototype chain)
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
Understanding of mongodb