原串与其逆序串 对应位置且 连续最大长度

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;

int main()
{
    char s[1000];
	char t[1000];
	int len;
    int i, j;

	memset(s, ‘\0‘, sizeof(s));
	memset(t, ‘\0‘, sizeof(t));

	while(gets(s)!=NULL )
	{

        len = strlen(s);

        for( i=0; i<len; i++)
		{
            t[len-i-1] = s[i] ;
		}
	//	puts(s);
	//	puts(t);
		int max=0;
		int cnt=0;

		for(i=0; i<len; )
		{
           if( s[i]==t[i] )
		   {
			   j=i;
			   while( s[j]==t[j] && j<len )
			   {
				   cnt++;
				   j++;
			   }
			   if(cnt>max)
			   {
				   max = cnt;  //max的值进行更新
				   cnt=0;
			   }
			   i = j ;
		   }
		   else
		   {
			   i++;
		   }
		}
		printf("%d\n", len-max );
	}
 	return 0;
}
时间: 2024-08-08 19:18:03

原串与其逆序串 对应位置且 连续最大长度的相关文章

266-字符串逆序输出-nyoj-20140808

#include <stdio.h>#include <string.h>int main(){    int T;    scanf("%d",&T);     getchar();    while(T--)    {        char str[45],b[45];        int m,k,i;        k=0;        //scanf("%s",str);        gets(str);       

C++刷题——2286: 逆序输出单词(串)

/* Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作者:陈丹妮 * 完成日期:2015年 5 月 25 日 * 版 本 号:v1.0 */ Description 输入一个字符串,空格作为单词分隔符,统计其中的单词,并将单词逆序输出 Input 输入一个字符串 Output 逆序输出单词 Sample Input I love you! Sample Output you love I! #incl

ACM-字符串逆序输出

描述 给定一行字符,逆序输出此行(空格.数字不输出) 输入 第一行是一个整数N(N<10)表示测试数据的组数) 每组测试数据占一行,每行数据中间有且只有一个空格(这样你可以把此行当成两个字符串读取). 每行字符长度不超过40 并且保证输入的字符只有空格(1个),数字,小写字母三种 输出 对应每行测试数据,逆序输出(空格和数字不输出) 样例输入 3 abc 123de abc 123 abc d 样例输出 edcba cba dcba 代码: #include<iostream> #inc

(hdu step 4.1.5)find the nth digit(求S串中的第n个位置上是什么数字)

题目: find the nth digit Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 689 Accepted Submission(s): 244   Problem Description 假设:S1 = 1S2 = 12S3 = 123S4 = 1234.........S9 = 123456789S10 = 123456789

给出一个不多于5位的整数,要求 1、求出它是几位数 2、分别输出每一位数字 3、按逆序输出各位数字,例如原数为321,应输出123

题目描述 输入 一个不大于5位的数字 输出 三行 第一行 位数 第二行 用空格分开的每个数字,注意最后一个数字后没有空格 第三行 按逆序输出这个数 样例输入 12345 样例输出 5 1 2 3 4 5 54321程序:#include<stdio.h>#include<math.h>int main(){    int a,c,d,count=0,t;    scanf("%d",&a);    d=c=a;    while(c!=0)    {  

C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等

上学时我们很多学了很多种排序算法,不过在c++stl中也封装了sort等函数,头文件是#include <algorithm> 函数名 功能描述 sort 对给定区间所有元素进行排序 stable_sort 对给定区间所有元素进行稳定排序 partial_sort 对给定区间所有元素部分排序 partial_sort_copy 对给定区间复制并排序 nth_element 找出给定区间的某个位置对应的元素 is_sorted 判断一个区间是否已经排好序 partition 使得符合某个条件的元

Extend to Palindrome (顺序Hash和逆序Hash处理回文)

题目链接:https://vjudge.net/contest/344930#problem/E 题目大意:给出一个字符串,在末尾补充最少的字母,使其整个成为一个回文串 题目思路:对字符串进行顺序Hash和逆序Hash,然后去枚举位置,如果此时顺序的Hash和逆序的Hash值想等就说明此时是一个回文串.然后就直接输出该回文串前面的部分,再输出该回文串.如果没有回文串,那么就使整个字符串成为回文串 1 #include <stdio.h> 2 #include <algorithm>

字符串-04. 字符串逆序

1 /* 2 * Main.c 3 * D4-字符串-04. 字符串逆序 4 * Created on: 2014年8月19日 5 * Author: Boomkeeper 6 *******测试通过****** 7 */ 8 9 #include <stdio.h> 10 11 int main(void){ 12 13 char str[80]; 14 15 gets(str); 16 17 int i; 18 for(i=0;i<80;i++){ 19 if(str[i]=='\0

C++刷题——2568: 字符串逆序输出

2568: 字符串逆序输出 /* Copyright (c) 2014, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作者:陈丹妮 * 完成日期:2015年 5 月 26 日 * 版 本 号:v1.0 */ Description 编写一个函数,功能是使输入的字符串逆序输出. Input 输入一串字符串,注意字符串中不要有空格. Output 输出该字符串的逆序. Sample Input ABCDEFG Sample Output GF