暴力
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
bool m_strstr(char* des, char* src, int n)
{
int tmp = 0;
char* p = src;
while (*des)
{
if (*des == *p)
{
des++;
p++;
tmp++;
if (tmp == n) return true;
continue;
}
des++;
p = src;
}
return false;
}
int main()
{
char str1[100] = { 0 };
char str2[100] = { 0 };
scanf("%s%s", str1, str2);
int len1 = strlen(str1);
int len2 = strlen(str2);
char* p = str1, * q = str2;//p指向短的一个字符串
if (len1 > len2)
{
p = str2, q = str1;
swap(len1, len2);
}
int cnt = 0;//字串的大小
char* res = NULL;//字串的起始位置
for (int i = 1;i <= len1; i++)
{
for (int j = 0; j < len1-i; j++)
{
if (m_strstr(q, p + j, i))//匹配成功
{
cnt = max(cnt, i);//更新
if (cnt == i)
{
res = p + j;
}
}
}
}
if (res != NULL)
{
for (int i = 0; i < cnt; i++) putchar(*res++);
}
return 0;
}