找到最大回文子串

Problem statement:

问题陈述:

Given a string, you have to find the largest palindromic substring using O(1) space complexity.

给定一个字符串,您必须使用O(1)空间复杂度找到最大的回文子字符串。

    Input:
T Test case
T no of input string will be given to you.
E.g.
3
abcsouuoshgcba
includeaedulcin
aaaaa
Constrain
1≤ length (string) ≤100
Output:
Print the largest palindromic substring form the given string.

Example

    T=3
Input:
abcsouuoshgcba
Output:
souuos
Input:
includeaedulcin
Output:
cludeaedulc
Input:
aaaaa
Output:
aaaaa

Explanation with example:

举例说明:

Let there is a string str.

让我们有一个字符串str 。

Now possible arrangements are:

现在可能的安排是:

  1. Single middle characters like aba

    像aba这样的单个中间字符

  2. Paired middle characters like bb

    配对的中间字符,如bb

To find the largest palindromic substring we follow these steps,

要找到最大的回文子串,请按照以下步骤操作,

  1. We start with the first index and go to the end of the string.

    我们从第一个索引开始,然后到字符串的末尾。

  2. Every time we take two variables

    每次我们取两个变量

  3. For the first possible arrangement, we initialize the first variable with the previous index of the current index and initialize the second variable with the next index of the current index.

    对于第一种可能的安排,我们使用当前索引的上一个索引初始化第一个变量,并使用当前索引的下一个索引初始化第二个变量。

  4. For the second possible arrangement, we initialize the first variable with the current index and initialize the second variable with the next variable of the current index.

    对于第二种可能的安排,我们用当前索引初始化第一个变量,并用当前索引的下一个变量初始化第二个变量。

  5. If the character at the first variable place is equal with the character at the second variable place then every time we decrease the first index by one and increase the second index by one and continue the process until or unless the first variable value will be greater than or equals to zero and the second variable value will be less than the length of the string.

    如果第一个变量位置的字符与第二个变量位置的字符相等,则每次我们将第一个索引减小一个,然后将第二个索引增大一个,然后继续执行该过程,直到或除非第一个变量值大于或等于零,并且第二个变量值将小于字符串的长度。

  6. Every time we will add up two with the length of the palindrome substring count.

    每次我们将用回文子串计数的长度加起来两个。

  7. If the character at both the variable place is not the same then we compare with the palindrome substring length.

    如果两个变量位置的字符都不相同,则将其与回文子串的长度进行比较。

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
string count_palindrom(string str)
{int len = str.length();
int max_length = 0;
int start = 0;
for (int i = 0; i < len; i++) {int j = i - 1;
int k = i + 1;
int count = 1;
while (j >= 0 && k < len) {if (str[j] == str[k]) {count += 2;
if (max_length < count) {max_length = count;
start = j;
}
j--;
k++;
}
else {break;
}
}
j = i;
k = i + 1;
count = 0;
while (j >= 0 && k < len) {if (str[j] != str[k])
break;
else {count += 2;
if (max_length < count) {max_length = count;
start = j;
}
j--;
k++;
}
}
}
string s = "";
if (start == 0 && max_length == 0) {return s + str[0];
}
for (int i = 0; i < max_length; i++) {s += str[start + i];
}
return s;
}
int main()
{//code
int t;
cout << "Test Case : ";
cin >> t;
while (t--) {string str;
cout << "Enter the string : ";
cin >> str;
cout << "The palindromic substrings is : " << count_palindrom(str) << endl;
}
return 0;
}

Output

输出量

Test Case : 3
Enter the string : abcsouuoshgcba
The palindromic substrings is : souuos
Enter the string : includeaedulcin
The palindromic substrings is : cludeaedulc
Enter the string : aaaaa
The palindromic substrings is : aaaaa

翻译自: https://www.includehelp.com/icp/find-the-largest-palindromic-substring-using-o-1-space-complexity.aspx

找到最大回文子串

找到最大回文子串_使用O(1)空间复杂度找到最大的回文子串相关推荐

  1. 扫描二维码读取文档_使用深度学习读取和分类扫描的文档

    扫描二维码读取文档 To many people's dismay, there is still a giant wealth of paper documents floating out the ...

  2. activiti 文档_那些可多人协作编辑的在线文档工具

    最近使用了下石墨文档,简直就是在线版的Word,Excel,PPT,而且可以多人实时编辑预览,真的太好用了,搜索了下发现这种在线文档工具还挺多的,这里做个整理推荐. 石墨文档 这个很多人应该都用过,石 ...

  3. 500个爆文标题_我研究了999篇100万+爆文,终于发现这10条标题规律!

    易撰 文章想要"爆",标题一定要妙! 标题是文章的眉目,是文章内容和读者情感之间的第一个接触点,是让人一见钟情的因子. 一个好的题目,能激起读者点击阅读的兴趣,有着眉目传神之妙用. ...

  4. 开发接口文档_更优更稳更好,看文档驱动开发模式在AIMS中的优势

    ​[摘要]程序员常会说:我最讨厌别人写的代码没有文档,我也最讨厌自己需要写文档. 有一个很老的梗: 我最讨厌别人写的代码没有文档,我也最讨厌自己需要写文档. 有这种想法的程序员应该算是一个老鸟了,对于 ...

  5. java栈和队列验证回文串_栈和队列的基本操作及其应用(回文判断)

    实验二栈和队列的基本操作及其应用 一.实验目的 1.掌握栈和队列的顺序存储结构和链式存储结构,以便在实际中灵活应用. 2.掌握栈和队列的特点,即后进先出和先进先出的原则. 3.掌握栈和队列的基本运算, ...

  6. 童星养成系统的文推荐_几部养成系列的现言宠文推荐啦,都是不错的大叔文,十分治愈哦~...

    哈喽,大家好,欢迎来到小编的百家号,今天小编带给大家的是几部养成系列的现言宠文,都是不错的大叔文,十分治愈哦~ <焰火热吻>荣槿 焰火热吻 文案: [年龄差/养成系/双向暗恋]刑侦 大队队 ...

  7. activiti 文档_免费、开源、多平台的PDF文档处理软件——PDFsam Basic

    今天给大家推荐的是一款免费.开源.多平台支持的PDF文档处理软件-- PDFsam Basic PDFsam Basic是为普通用户提供的免费开源解决方案,提供了PDF文档拆分.合并.混合.提取页面和 ...

  8. mysql chm 文档_最简单,最实用的数据库CHM文档生成工具——DBCHM

    推荐文章 1.SQLite: SELECT 'AA' || 'BB' || 'CC'; 2.MySQL: SELECT CONCAT('AA', 'BB', ' 推荐文章 QT += sql QSql ...

  9. java 图片 word文档_【Java】用Freemarker完美导出word文档(带图片)

    Java  用Freemarker完美导出word文档(带图片) 前言 最近在项目中,因客户要求,将页面内容(如合同协议)导出成word,在网上翻了好多,感觉太乱了,不过最后还是较好解决了这个问题. ...

  10. java 显示word文档_如何通过apache POI技术来读取Word文档,并把Word文档的原来格式完整地显示在html网页?...

    我把代码发到你的邮箱. package com.my.util; import java.io.BufferedWriter; import java.io.File; import java.io. ...

最新文章

  1. Memcached 1.5.13 发布,支持 TLS
  2. jquery动态加载JS【方法getScript】的改进
  3. jQuery中操作元素节点appendTo()与prependTo()的区别
  4. 你真的了解Java中的三目运算符吗
  5. Android开发中依赖注入的应用
  6. Not so Mobile UVA - 839
  7. Kvm虚拟化性能测试与性能优化实践
  8. Spring框架的本质:3了解一点儿JavaConfig
  9. jquery 使用文档
  10. 前端es6文档大全,你想要的这都有
  11. R语言软件和RStudio环境的下载与安装
  12. android:launchMode=“singleTask“
  13. R语言(pROC)绘图
  14. 今日分享——点点小事对人的影响
  15. 怎么退出用户登录linux,怎样登录和退出Linux系统
  16. kafka部署时出现的天坑
  17. 柔性作业车间调度问题 (FJSSP)
  18. 抖音视频二创闷声发财
  19. brew php 降级icu4c
  20. 简单搭建微服务springCloudNetflix服务(一)

热门文章

  1. 干加个偏旁可以变成什么字_面试官:“干”字加一笔,变成什么字?回答王和午字不对...
  2. React组件实现越级传递属性
  3. win主机ping不通linux的IP
  4. Hbuilder MUI 下拉选择与时间选择器
  5. Spring-framework应用程序启动loadtime源码分析笔记(二)——@Transactional
  6. 网站后台中对html标签的处理
  7. ubuntu 14.04 java_Ubuntu14.04下配置Java环境
  8. 服务器芯片镜像测试,模拟镜像服务器磁盘问题的两个测试【转】
  9. ddr5内存上市时间_DDR5内存明年才能上市,SK Hynix已预研DDR6:12Gbps
  10. oracle stream 主键,oracle stream配置向导