nyoj 308 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

2015,4,17

最长公共子序列

#include<stdio.h>
#include<string.h>
char s[55],c[55];
int a[55][55];
int main(){
	int t,i,j,k,len,m;
	scanf("%d",&t);
	while(t--){
		m=0;
		memset(a,0,sizeof(a));
		scanf("%s",s);
		len=strlen(s);
		for(i=0,j=len-1;i<len;i++,j--)//翻转字符串
			c[i]=s[j];
		for(i=1;i<=len;i++)
			for(j=1;j<
			=len;j++)
				if(s[i-1]==c[j-1]){
					a[i][j]=a[i-1][j-1]+1;//a[i][j]代表如果s[i-1]=c[j-1]的话,那么a[i][j]就等于a[i-1][j-1]+1;
					if(m<a[i][j]){//因为如果前两个不匹配,那么a[i-1][j-1]就是0,那么a[i][j]就是1;如果前边两个匹配
						m=a[i][j];//那么a[i][j]就等于a[i-1][j-1]再加 1,就是2,a[i][j]就是指到ij的时候有几个字符匹配
						k=i;//然后记录位置,输出就可以了,,这个过程如果还不懂就模拟一下,我也是看大神代码模拟一遍才理解
					}
				}
		for(i=k-m;i<k;i++)
			printf("%c",s[i]);
		printf("\n");
	}
	return 0;
}
时间: 2024-10-10 19:11:53

nyoj 308 Substring的相关文章

[河南省ACM省赛-第四届] Substring (nyoj 308)

练习使用字符串函数了. 1.字串的反转也是它的字串,2.最长,3.最先出现 string: #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; int main() { int t, n; string s; cin>>t; while(t--){ cin>&

NYOJ 1067 Compress String(区间dp)

Compress String 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描述 One day,a beautiful girl ask LYH to help her complete a complicated task-using a new compression method similar to Run Length Encoding(RLE) compress a string.Though the task is difficult, LYH is

NYOJ 483 Nightmare 【广搜】+【无标记】

Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial e

indesOf.substr,substring,charAt的区别

var a = "asdfghjkl" alert(a.substr(1, 3));        //  从下标为1开始,往右数3个长度的数, 显示 sdf; alert(a.substring(1, 3));   //  从下标为1开始,到下标为3结束(左闭右开), 显示 sd; alert(a.indexOf("s", 1));   //  找"s",从下标为1开始找,找到显示下标,找不到显示-1, 显示 1: alert(a.charAt

Longest Substring Without Repeating Characters

最长无重复字符的子串 第一次提交:没有考虑字符串为空的情形.错误. 第二次提交:AC 思路: 1.判断字符串是否为空,若非空,进行下一步: 2.定义一个 和字符串等长的整形数组 result[] 和一个 字符数组. 整形数组 用于存放从每个字符开始计算 无重复字符子串的长度,字符数组用于存放字符串(getChars方法) 3.双重循环.外层循环 len 次,内层循环每次计算result[i] 的值.判断当前字符是否  在开始字符到当前字符之前的一个字符中间的字符串中出现过,若未出现过,resul

3 Longest Substring Without Repeating Characters

1 public int lengthOfLongestSubstring(String s) { 2 long length = s.length(); 3 String tmp = ""; 4 int substringLength = 0; 5 for (int i = 0; i < length; i ++) { 6 if (tmp.contains(("" + s.charAt(i)))) { 7 if (tmp.length() > subs

LeetCode 459. Repeated Substring Pattern

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 100

[string]Longest Palindromic Substring

Total Accepted: 82026 Total Submissions: 379898 Difficulty: Medium 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. Subscrib

LeetCode 30 Substring with Concatenation of All Words(与所有文字串联子串)(*)

翻译 给定一个字符串S,一个单词的列表words,全是相同的长度. 找到的子串(多个)以s即每个词的字串联恰好一次并没有任何插入的字符所有的起始索引. 原文 You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word