鱼C论坛

 找回密码
 立即注册
查看: 1773|回复: 1

[已解决]各位大佬帮忙看看这道题用c语言或c++怎么写?

[复制链接]
发表于 2022-6-4 15:33:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回文字符串就是从前往后读和从后往前读都一样的字符串。比如 abcba, aaa, QoQ。现在给你一个字符串,你可以从中删去一些字符,在不改变原来字符相对顺序的情况下,得到一个最长的回文字符串。

比如 abxdba, 删去字符 x 后,可以得到 abdba,是一个回文字符串。你的任务就是求出给定的字符串删去若干字符后可以得到的最长的回文字符串的长度。字符串长度不超过 1000,字符范围为 ‘a’ 到 ‘z’。

输入描述
有多组输入。

每组一行字符串。

输出
每组测试数据输出一个整数,表示可以得到的最长的回文字符串的长度。

样例输入
aabbaabb

computer

abzla

samhita

样例输出
6

1

3

3
最佳答案
2022-6-4 19:41:53
本帖最后由 jhq999 于 2022-6-4 21:19 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main ()
  4. {
  5.         int i=0,j=0,k=0,len=0,count=0,max1=0,max2=0,half=0,r=0;
  6.         char str[1001]={0};
  7.         scanf("%s",str);
  8.         for(len=0;str[len];len++);
  9.         for(i=0;i<len;i++)//////
  10.         {
  11.                 count=0;
  12.                 r=i+1;
  13.                 for(j=i;j>=0;j--)
  14.                 {
  15.                         for(k=r;k<len;k++)
  16.                         {                        
  17.                                 if(str[k]==str[j])
  18.                                 {
  19.                                         count++;
  20.                                         r=k+1;
  21.                                         break;
  22.                                         }
  23.                                 
  24.                                 }
  25.                                 
  26.                         }
  27.                         printf("%d  %d,",count,i);
  28.                         if(max1<=count)max2=max1,max1=count,half=i;
  29.                 }
  30.                 max1*=2;
  31.                 max2*=2;
  32.                
  33.                 if(max2==max1)max1+=1;
  34.                 printf("%d %d  %d",len,max1,max2);
  35.                        
  36.         return 0;
  37.         
  38.         }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-6-4 19:41:53 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2022-6-4 21:19 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main ()
  4. {
  5.         int i=0,j=0,k=0,len=0,count=0,max1=0,max2=0,half=0,r=0;
  6.         char str[1001]={0};
  7.         scanf("%s",str);
  8.         for(len=0;str[len];len++);
  9.         for(i=0;i<len;i++)//////
  10.         {
  11.                 count=0;
  12.                 r=i+1;
  13.                 for(j=i;j>=0;j--)
  14.                 {
  15.                         for(k=r;k<len;k++)
  16.                         {                        
  17.                                 if(str[k]==str[j])
  18.                                 {
  19.                                         count++;
  20.                                         r=k+1;
  21.                                         break;
  22.                                         }
  23.                                 
  24.                                 }
  25.                                 
  26.                         }
  27.                         printf("%d  %d,",count,i);
  28.                         if(max1<=count)max2=max1,max1=count,half=i;
  29.                 }
  30.                 max1*=2;
  31.                 max2*=2;
  32.                
  33.                 if(max2==max1)max1+=1;
  34.                 printf("%d %d  %d",len,max1,max2);
  35.                        
  36.         return 0;
  37.         
  38.         }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 05:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表