/*
一串首尾相连的珠子(m 个),有 N 种颜色(N<=10),
设计一个算法,取出其中一段,要求包含所有 N 中颜色,并使长度最短。
*/
#include
#include
#include
int shortestlengh(char * in, char ** dst, int N)
{
//变成inin的形式,避免求余
int nlen = strlen(in);
char * in2 = (char *)malloc(2 * nlen * sizeof(char));
memcpy(in2, in, nlen * sizeof(char));
memcpy(in2 + nlen, in, nlen * sizeof(char));
int start = 0, end = nlen - 1;
int shortestlen = nlen;
int hash[256] = {0};
int colornum = 0;
int s = 0, e = -1;
//遍历所有可能的起始点
while(s < nlen)
{
while(colornum < N && e <= 2 * nlen) //找到在当前起点下找到所有颜色的结尾
{
e++;
if(hash[int(in2[e])] == 0)
{
colornum++;
}
hash[int(in2[e])]++;
}
//去掉前面相同的部分
while(in2[s] == in2[s + 1])
{
s++;
hash[(int)in2[s]]--;
搭蠢 }
//更新最短的串
if(shortestlen > e - s + 1)
{
shortestlen = e - s + 1;
start = s;
end = e;
搜枝弊 }
//更新s,从下一个颜色开始
hash[(int)in2[s]]--;
if(hash[(int)in2[s]] == 0)
{
colornum--;
}
s = s + 1;
}
*(dst) = (char *)malloc(end - start + 2);
memcpy(*dst, in2 + start, end - start + 1);
(*dst)[end - start + 1] = '\0'; //注意
free(in2);
return end - start + 1;
}
int main()
{
char * s = 世族"addcddcbccbba";
char * d = NULL;
int n = shortestlengh(s, &d, 4);
printf("%d\n%s\n", n, d);
return 0;
}