题目:
给一个很长的字符串str 还有一个字符集比如{a,b,c} 找出str 里包含{a,b,c}的最短子串。要求O(n).
比如,字符集是a,b,c,字符串是abdcaabcx,则最短子串为abc。
思路一:
用两个变量Front, Rear 指向这个字串(str)区间的头和尾,开始的时候同指向字符串首地址,用一个变量int achSubCnt[256]={0}记录字符集a,b,c 个数,用另外一个变量int achCnt[256] = {0}记录当前这个子串里a,b,c各自出现的个数,一个变量i32CurSum记录当前Front,Rear指向的区间中有多少个字符集里面的元素了,Rear 一直加,然后判断Rear所指的字符是否是在字符集中,如果是那么更新achCnt[]和i32CurSum的值,直到i32CurSum等于字符集个数,然后Front++,直到achCnt[]里某个字符个数为0,这样就找到一个符合条件的字串了。
继续前面的操作,就可以找到最短的了。(如果题目要求把所有匹配字符集中的字符串罗列出来也是可以的,这样还可以不进行匹配字串长度的比较)
代码如下:
/*--------------------------------
Copyright by yuucyf. 2011.08.24
---------------------------------*/
#include "stdafx.h"
#include <iostream>
using namespace std;
#define CHAR_COUNT 256
typedef struct tagPosition
{
int i32Front;
int i32Rear;
tagPosition()
{
i32Front = i32Rear = 0;
}
}S_StrPos;
bool FindShortSubString(const char* pszStr, const char *pszSubStr, S_StrPos &sShortSubStr)
{
if (NULL == pszStr || NULL == pszSubStr)
return false;
int i32Len = _tcslen(pszStr);
int i32SubLen = _tcslen(pszSubStr);
if (i32Len <= 0 || i32SubLen <= 0)
return false;
sShortSubStr.i32Front = sShortSubStr.i32Rear = 0;
int i32CurSum = 0;
int achSubCnt[CHAR_COUNT] = {0};
int achCnt[CHAR_COUNT] = {0};
int i32I = 0;
for (i32I = 0; i32I < i32SubLen; i32I++)
{
achSubCnt[pszSubStr[i32I]]++;
}
int Front = 0, Rear = 0;
while (Front < i32Len && Rear < i32Len)
{
if (i32CurSum != i32SubLen)
{
if (achSubCnt[pszStr[Rear]] > 0)
{
achCnt[pszStr[Rear]]++;
if (achSubCnt[pszStr[Rear]] >= achCnt[pszStr[Rear]])
i32CurSum++;
}
Rear++;
}
else
{
achCnt[pszStr[Front]]--;
if (achCnt[pszStr[Front]] == 0) //Find a sub string.
{
if (sShortSubStr.i32Rear == 0)
{
sShortSubStr.i32Front = Front;
sShortSubStr.i32Rear = Rear;
}
else if ((sShortSubStr.i32Rear - sShortSubStr.i32Front) > (Rear - Front))
{
sShortSubStr.i32Front = Front;
sShortSubStr.i32Rear = Rear;
}
i32CurSum--;
}
Front++;
}
}
//可能是Rear已经到达字符串尾,可是还存在i32CurSum == i32SubLen的情况,
//所以要从Front开始往前遍历,找到最后一个满足条件的字串。
while (i32CurSum == i32SubLen)
{
achCnt[pszStr[Front]]--;
if (achCnt[pszStr[Front]] == 0) //Find a sub string.
{
if (sShortSubStr.i32Rear == 0)
{
sShortSubStr.i32Front = Front;
sShortSubStr.i32Rear = Rear;
}
else if ((sShortSubStr.i32Rear - sShortSubStr.i32Front) > (Rear - Front))
{
sShortSubStr.i32Front = Front;
sShortSubStr.i32Rear = Rear;
}
i32CurSum--;
}
Front++;
}
return (sShortSubStr.i32Rear != 0);
}
int _tmain(int argc, _TCHAR* argv[])
{
char aszStr[] = "abdcaabcx";
char aszSubStr[] = "abc";
S_StrPos sStrPos;
if (FindShortSubString(aszStr, aszSubStr, sStrPos))
{
cout << "最短的字串为:";
for (int i32I = sStrPos.i32Front; i32I < sStrPos.i32Rear; i32I++)
cout << aszStr[i32I];
cout << endl;
}
return 0;
}
给定一个长字符串str和字符集如{a, b, c},目标是找出包含字符集的最短子串。采用双指针法,使用Front和Rear指向字符串区间,用两个数组achSubCnt和achCnt分别记录字符集和子串中字符的出现次数,当i32CurSum等于字符集大小时,Front前移,直至找到最短子串。"
95211032,8669732,使用ggplotly()美化ggplot2图像,"['r语言', '数据可视化', 'plotly']
给一个很长的字符串str 还有一个字符集比如{a,b,c} 找出str 里包含{a,b,c}的最短子串。要求On.&spm=1001.2101.3001.5002&articleId=6714235&d=1&t=3&u=bb7ddf8cb352470fb9d0dc77a7ee48b7)
1112

被折叠的 条评论
为什么被折叠?



