找出字符串的最长子串,要求子串的所有字符相同 例如:str ="sssddddabcdef" 则输出字串为:dddd

该博客探讨如何在给定字符串中找到最长的子串,条件是子串中的所有字符都相同。例如,对于字符串"sssddddabcdef",最长子串是"dddd"。文章可能涉及C++编程解决方案和相关算法思想。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/****************************************************************
找出字符串的最长子串,要求子串的所有字符相同
例如:str ="sssddddabcdef" 则输出字串为:dddd
*****************************************************************/
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<iostream>
void GetSubstring(char* strSource,char *strSubstring)
{

  int nLen;      //源字符串长度
  int nCurPos;   //当前统计字符串的头指针位置(相对于原字符串第一个字符的位置)
  int nCurCount; //当前统计字符串的长度(有相同字符组成的子字符串)
  int nPos;     //当前最长的子串的头指针位置
  int nCount;    //当前最长的子串的长度
  int i;
  nLen = strlen(strSource);

  //初始化变量
  nCount = 1;
  nPos = 0;
  nCurCount = 1;
  nCurPos = 0;

  //遍历整个字符串
  for(i = 1; i < nLen; i++)
  {
    if(strSource[i] == strSource[nCurPos]&&!isblank(strSource[i]))//如果当前字符与子串的字符相同,子串长度增1
      nCurCount++;
    else  //如果不相同,开始统计新的子串
    {
      if(nCurCount > nCount)//如果当前子串比原先最长的子串长,把当前子串信息(起始位置 + 长度)保留下来
      {
        nCount = nCurCount;
        nPos = nCurPos;
      }
      //长度复值为1,新串的开始位置为i
      nCurCount = 1;
      nCurPos = i;
    }
  }

  //为返回的子串分配空间(长度为nCount,由于要包括字符串结束符\0,故大小要加1)

  /*****************复制子串(用其他函数也可以)*********
  for(i = 0; i < nCount; i++)
    strSubstring[i] = strSource[nPos + i];
  strSubstring[nCount] = '\0';
  ******************************************************/
  strncpy(strSubstring,strSource+nPos,nCount); //注意 strcnpy  危险之处在于不能自动处理字符串结束标志
  strSubstring[nCount]='\0';


}
void test()
{
  //输入一个字符串strSource
    char *strSource=
    "To recap, the three main objectives in the Mystery Method are: \n\
    To attract a woman \n\
    To establish comfort, trust, and connection \n\
    To structure the opportunity to be seduced ";
    char *output=new char[strlen(strSource)+1];
    GetSubstring(strSource,output);
    printf("字符串为:%s\n",strSource);
    printf("最长子串为:%s\n长度为:%d\n",output,strlen(output));
    delete []output;

}
int main()
{
    printf("-------test()-----------\n");
    test();
    printf("-------test in main()-----------\n");
    //输入一个字符串strSource
    char *strSource="sssddddabcdef";
    char *output=new char[strlen(strSource)+1];
    GetSubstring(strSource,output);
    printf("字符串为:%s\n",strSource);
    printf("最长子串为:%s\n长度为:%d\n",output,strlen(output));
    delete []output;
}
/************************************************
程序运行结果:
-------test()-----------
字符串为:To recap, the three main objectives in the Mystery Method are:
    To attract a woman
    To establish comfort, trust, and connection
    To structure the opportunity to be seduced
最长子串为:ee
长度为:2
-------test in main()-----------
字符串为:sssddddabcdef
最长子串为:dddd
长度为:4

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.
/****************************************************************
找出字符串的最长子串,要求子串的所有字符相同
例如:str ="sssddddabcdef" 则输出字串为:dddd
*****************************************************************/
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<iostream>
void GetSubstring(char* strSource,char *strSubstring)
{

  int nLen;      //源字符串长度
  int nCurPos;   //当前统计字符串的头指针位置(相对于原字符串第一个字符的位置)
  int nCurCount; //当前统计字符串的长度(有相同字符组成的子字符串)
  int nPos;     //当前最长的子串的头指针位置
  int nCount;    //当前最长的子串的长度
  int i;
  nLen = strlen(strSource);

  //初始化变量
  nCount = 1;
  nPos = 0;
  nCurCount = 1;
  nCurPos = 0;

  //遍历整个字符串
  for(i = 1; i < nLen; i++)
  {
    if(strSource[i] == strSource[nCurPos]&&!isblank(strSource[i]))//如果当前字符与子串的字符相同,子串长度增1
      nCurCount++;
    else  //如果不相同,开始统计新的子串
    {
      if(nCurCount > nCount)//如果当前子串比原先最长的子串长,把当前子串信息(起始位置 + 长度)保留下来
      {
        nCount = nCurCount;
        nPos = nCurPos;
      }
      //长度复值为1,新串的开始位置为i
      nCurCount = 1;
      nCurPos = i;
    }
  }

  //为返回的子串分配空间(长度为nCount,由于要包括字符串结束符\0,故大小要加1)

  /*****************复制子串(用其他函数也可以)*********
  for(i = 0; i < nCount; i++)
    strSubstring[i] = strSource[nPos + i];
  strSubstring[nCount] = '\0';
  ******************************************************/
  strncpy(strSubstring,strSource+nPos,nCount); //注意 strcnpy  危险之处在于不能自动处理字符串结束标志
  strSubstring[nCount]='\0';


}
void test()
{
  //输入一个字符串strSource
    char *strSource=
    "To recap, the three main objectives in the Mystery Method are: \n\
    To attract a woman \n\
    To establish comfort, trust, and connection \n\
    To structure the opportunity to be seduced ";
    char *output=new char[strlen(strSource)+1];
    GetSubstring(strSource,output);
    printf("字符串为:%s\n",strSource);
    printf("最长子串为:%s\n长度为:%d\n",output,strlen(output));
    delete []output;

}
int main()
{
    printf("-------test()-----------\n");
    test();
    printf("-------test in main()-----------\n");
    //输入一个字符串strSource
    char *strSource="sssddddabcdef";
    char *output=new char[strlen(strSource)+1];
    GetSubstring(strSource,output);
    printf("字符串为:%s\n",strSource);
    printf("最长子串为:%s\n长度为:%d\n",output,strlen(output));
    delete []output;
}
/************************************************
程序运行结果:
-------test()-----------
字符串为:To recap, the three main objectives in the Mystery Method are:
    To attract a woman
    To establish comfort, trust, and connection
    To structure the opportunity to be seduced
最长子串为:ee
长度为:2
-------test in main()-----------
字符串为:sssddddabcdef
最长子串为:dddd
长度为:4

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.

*************************************************/


*************************************************/





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值