题目
给你一个字符串 s,字符串s首尾相连成一个环形 ,请你在环中找出'l'、'o'、'x' 字符都恰好出现了偶数次最长子字符串的长度。
输入描述
输入是一串小写的字母组成的字符串s。
1 <= s.length <= 5 x 10^5
s 只包含小写英文字母。
输出描述
输出是一个整数
用例1
输入
alolobo
输出
6
说明:最长子字符串之一是 "alolob",它包含 'l','o'各 2 个,以及 0 个 'x' 。
C++代码如下:
#include <stdio.h>
#include <map>
#include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
class CFindMaxSubStr
{
public:
CFindMaxSubStr(string strParent,vector<char> vecKey)
{
m_strParent = strParent;
m_vecKey = vecKey;
m_pBuffer = new char[m_strParent.size()];
memset(m_pBuffer,0x00,m_strParent.size());
FindMaxSubByKey();
}
string GetMaxSubStr()
{
return m_strSub;
}
protected:
bool FindMaxSubByKey()
{
if (m_vecKey.empty())
{
m_strSub = m_strParent;
return true;
}
if (m_strParent.length() == 0)
{
return false;
}
for(int indexBase = 0; indexBase < m_strParent.length(); indexBase++)
{
map<char,int> mapFindRes;
memset(m_pBuffer,0x00,m_strParent.size());
for (size_t sCurSize = 0; sCurSize < m_vecKey.size();sCurSize++)
{
mapFindRes[m_vecKey[sCurSize]] = 0;
}
for (int indexFind = 0; indexFind < m_strParent.length(); indexFind++)
{
int iCurPos = (indexBase + indexFind) % m_strParent.length();
char cCurFind = *(m_strParent.c_str() + iCurPos);
m_pBuffer[indexFind] = cCurFind;
if (mapFindRes.find(cCurFind) != mapFindRes.end())
{
mapFindRes[cCurFind] += 1;
}
if (indexFind + 1 < m_strSub.length())
{
continue;
}
map<char,int>::iterator iterForLoop = mapFindRes.begin();
bool bMatch(true);
for (;iterForLoop != mapFindRes.end(); iterForLoop++)
{
if (iterForLoop->second % 2 != 0)
{
bMatch = false;
break;
}
}
if (bMatch)
{
if (indexFind + 1 > m_strSub.length())
{
m_strSub = m_pBuffer;
}
}
}
}
return true;
}
private:
string m_strParent;
string m_strSub;
vector<char> m_vecKey;
char* m_pBuffer;
};
int main()
{
string strSrc = "osfdsfsdfsdfd342342ooxxllasdadsasxdasd";
vector<char> vecKey;
vecKey.push_back( 'l');
vecKey.push_back( 'x');
vecKey.push_back( 'o');
CFindMaxSubStr oFindSub(strSrc,vecKey);
string strRet = oFindSub.GetMaxSubStr();
cout << "满足条件的最长字串是:"<< strRet << "最长长度:" << strRet.length() << endl;
return 0;
}