69,371
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
void search(char *str);
int main()
{
char a[100];
// char b[100];
gets(a);
search(a);
// printf("%s\n", b);
return 0;
}
void search(char *str)
{
int nLen = strlen(str);
char *p = (char*)malloc(nLen - 1);
int i;
for (i = nLen / 2; i > 0; i--)
{
memset(p, 0xff, nLen - 1);
int j;
aa for (j = 0; j < nLen - i; j++)
{
*(p + j) = *(str + j) - *(str + j + i);
}
int con_flag(0), find_flag(0), begin, end, count(0);
for (j = 0; j < nLen; j++)
{
if (*(p + j) == 0)
{
if (con_flag)
{
count++;
end = j;
}
else
{
begin = end = j;
con_flag = 1;
count = 1;
}
}
else
{
if (count >= i)
{
int k;
for (k = 0; k < count; k++)
{
putchar(*(str + begin + k));
}
putchar('\n');
begin = end = 0;
con_flag = 0;
count = 0;
find_flag = 1;
}
}
if (find_flag)
return;
}
}
}
#include <stdio.h>
int main()
{
char str[100];
int max = 0, length, start, tmp;
int i = 0, pos = 0, index = 0;
char ch;
gets(str);
while(str[i] != '\0')
{
length = 0;
tmp = 0;
while(str[i + 1] == str[i] + 1)//连续最长的字符串
{
++length;
++i;
}
if(length >= 1)
{
start = i + 1;
ch = str[i] - length;
tmp = length + 1;
while(tmp > 0)//是否是重复的字符串
{
if(str[start++] != ch)
break;
++ch;
--tmp;
}
if(tmp == 0 && length + 1 > max)//判断是不是最长的
{
max = length + 1;
index = i;
}
}
++pos;
i = pos;
}
//printf("max = %d index = %d\n", max, index);
while(max > 0)
{
printf("%c", str[++index]);
--max;
}
printf("\n");
return 0;
}