#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#include <string>
int main()
{
string str,temp;
cout<<"Please input the string:";
cin>>str;
int len = str.length();
for(int i=len-1; i>1; i--)
{
for(int j=0; j<len; j++)
{
if(i+j<len)
{
size_t t = 0;
size_t num = 0;
temp = str.substr(j,i);//从大到小取子串,从位置j开始,共i个字符
t = str.find(temp);
num = str.rfind(temp);
if(t != num)
{
cout<<temp<<t+1<<"\t"<<endl;
return 0;
}
}
}
}
cout<<"the longest substr is 1"<<endl;//如果每个字符都不同,则输出提示
return 0;
}
#include <string.h>
#include <iostream>
using namespace std;
#include <string>
int main()
{
string str,temp;
cout<<"Please input the string:";
cin>>str;
int len = str.length();
for(int i=len-1; i>1; i--)
{
for(int j=0; j<len; j++)
{
if(i+j<len)
{
size_t t = 0;
size_t num = 0;
temp = str.substr(j,i);//从大到小取子串,从位置j开始,共i个字符
t = str.find(temp);
num = str.rfind(temp);
if(t != num)
{
cout<<temp<<t+1<<"\t"<<endl;
return 0;
}
}
}
}
cout<<"the longest substr is 1"<<endl;//如果每个字符都不同,则输出提示
return 0;
}