代码题(56)— 最长重复子串、无重复字符的最长子串
1、最长的重复子串 寻找一个字符串中最长的重复子串 最大后缀方法思路: 1. 用字符串指针数组保存用户输入的字符串的 所有后缀字符串 ; 2. 将后缀字符串集合进行 排序 ; 3. 比较相邻字符串的公共子串长度 ,找到长度最大值,保存相应字符串即为所求 空间复杂度:求长度为n的字符串的后缀,需要O(n)的空间复杂度 时间复杂度:产生后缀数组-时间复杂度O(N)、对后缀数组排序是O(N*NlogN),第一个N表示字符串的比较,后面NlogN使用快排排序。依次检测相邻两个后缀的公共长度-时间复杂度O(N*N)、取出最大公共长度的前缀-时间复杂度O(N)。直接用sort排序,时间复杂度是nlog(n)。 总的时间复杂度是O(N*NlogN) #include<iostream> #include<algorithm> #include<stdio.h> #include <vector> #include<string> #include<sstream> #include<map> #include<set> #include <functional> // std::greater using namespace std; int getCommon(string s1, string s2) { int i = 0; for (; i < s1.size()&&