(nyoj308)substring

2024-09-08 02:38
文章标签 substring nyoj308

本文主要是介绍(nyoj308)substring,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Substring

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 1
描述

You are given a string input. You are to find the longest substring of input such that the reversal of the substring is also a substring of input. In case of a tie, return the string that occurs earliest in input. 

Note well: The substring and its reversal may overlap partially or completely. The entire original string is itself a valid substring . The best we can do is find a one character substring, so we implement the tie-breaker rule of taking the earliest one first.

输入
The first line of input gives a single integer, 1 ≤ N ≤ 10, the number of test cases. Then follow, for each test case, a line containing between 1 and 50 characters, inclusive. Each character of input will be an uppercase letter ('A'-'Z').
输出
Output for each test case the longest substring of input such that the reversal of the substring is also a substring of input
样例输入
3                   
ABCABA
XYZ
XCVCX
样例输出
ABA
X
XCVCX


这题很多人跪在题意上,表示我也贡献了一次wa,就是没读懂题意,这道题的意思是找最长子串反过来还是其子串,不是找最长的回文串,举个例子

abcdeba输出ab,因为ab是可以反过来的最长子串;

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstring>
#include<stdlib.h>
#include<ctype.h>
using namespace std;int main()
{string ch1;int t,maxlen=1,ans;cin>>t;while(t--){cin>>ch1;string ch2=ch1;reverse(ch2.begin(),ch2.end());bool flag=0;for(int i=ch2.size();i>0;i--){for(int j=0;j<=ch2.size()-i;j++){string v=ch1.substr(j,i);if(ch2.find(v)!=string::npos){cout<<v<<endl;flag=1;break;}}if(flag==1)break;}}
}


这篇关于(nyoj308)substring的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1146879

相关文章

MySQL中的`SUBSTRING()`和`MID()`函数:精准抽取字符串中的子串

在数据库操作中,经常需要从存储的字符串中提取出特定的部分,比如从用户全名中提取姓氏、从日期字符串中提取年份等。MySQL提供了SUBSTRING()和MID()两个函数,它们的功能几乎完全相同,都是用来从字符串中抽取子串的。本文将详细介绍这两个函数的用法、参数以及在实际场景中的应用。 一、SUBSTRING()和MID()函数的基本语法 1. SUBSTRING()函数 SUBSTRING(

mysql 的函数用法SUBSTRING_INDEX

因为数据库的数据要更新操作,内容是这样的: 这是之前的数据,现在因为需求变更,只需要横杠之前的数据,数据量少可以手动改,但是有几百条的数据,所以找到了一个方法 UPDATE product SET pro_price=SUBSTRING_INDEX(pro_price, '-', 1);  这个SUBSTRING_INDEX就是用来截取的 pro_price是要修改的字段名,然后中间

java常用算法之最长回文子串(Longest Palindromic Substring)

方法一:时间复杂度为O(n^3) public static String longestPalindrome1(String s) {int maxPalinLength = 0;String longestPalindrome = null;int length = s.length();// check all possible sub stringsfor (int i = 0; i

LeetCode 30 Substring with Concatenation of All Words

题意: 给出字符串s和许多等长(len)单词w,找出所有s中的满足子串为w中所有单词的一种组合的位置。 思路: 因为w中的单词要满足的是组合而不是排列,因此用“区间[L,R]中包含单词的计数”来维护比较合适。 一是满足了组合对顺序的不要求,二是方便处理重复的单词。 首先可以统计一下,w中各种单词个数。如果s的长度为size(w) * len的子串单词计数与w相同,则找到一个答案。

Longest Substring with At Most K Distinct Characters

Given a string, find the length of the longest substring T that contains at mostk distinct characters. For example,Given s = “eceba” and k = 2, T is "ece" which its length is 3. 思路:跟  Longest Sub

Longest Substring with At Most Two Distinct Characters

Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example,Given s = “eceba”, T is "ece" which its length is 3. 思路:同向双指针,跟Longest Substrin

LeetCode #3. Longest Substring Without Repeating Characters

题意: 计算一个字符串的中的最长的不含有重复字母的长度 解法: 尺取法的裸题了,维护2个指针l,r, 不断移动r指针,同时检查[l,r]是不是存在重复的了,如果存在就移动l指针了 class Solution {public:int lengthOfLongestSubstring(string s) {int n = s.size();int l=0, r=0;set<char> S;in

LeetCode | Longest Palindromic Substring

题目 Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 最长回文串问题,网上也有超

substring 函数

返回第一个参数中从第二个参数指定的位置开始、第三个参数指定的长度的子字符串。    string substring(string, number, number?)    备注 该字符串中的每个字符都被认为具有数字位置:第一个字符的位置是 1,第二个字符的位置是 2,依此类推。   如果未指定第三个参数,将返回从第二个参数指定的位置开始直到字符串结尾的子字符串。   如果参数不是

第五题:最长回文子串(Longest Palindromic Substring)

题目描述: 给定一个字符串 s,找到 s 中最长的回文子串。 示例: 输入:s = "babad" 输出:"bab" 或 "aba" 输入:s = "cbbd" 输出:"bb" 要求: 你需要找出 s 中的最长回文子串。 解题思路 方法1:中心扩展法 回文字符串的特点是对称的,因此我们可以从每个字符(或字符间隙)作为中心,向两边扩展来找到回文子串。 遍历每个字符:对于字符串