笔试试题 一枚

merlin_q 2011-09-01 04:12:12
题目:编写一个函数int search(char *text),text为输入的字符串,从字符串中找出一个最长的不含重复字符的子字符串,例如“axdbx”,返回4,子字符串为“axdb”,而“axdbxce”,返回5,子字符串为“dbxce”。时间复杂度为o(n).
...全文
235 12 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Doris_Good 2011-09-02
  • 打赏
  • 举报
回复
睡觉,我倒,这个问题自己想啊
shi3590 2011-09-02
  • 打赏
  • 举报
回复

int search(char *test)
{
if (test==NULL) return 0;
int map[256]={0};
int max=0,tmp=0;
int len=strlen(test);
for (int i=0;i<len;++i)
{
if (map[test[i]])
{
tmp=i-map[test[i]];
}
map[test[i]]=i+1;
++tmp;
max=max<tmp?tmp:max;
}
return max;
}

int main()
{
char test[]="axdbxce";
printf("%d\n",search(test));
return 0;
}
kkrmr 2011-09-02
  • 打赏
  • 举报
回复
帮顶。。
merlin_q 2011-09-02
  • 打赏
  • 举报
回复
p = p - length + 1;


好像有改进的空间
hacqing 2011-09-02
  • 打赏
  • 举报
回复

申请一个char cArray[26];接下来的你懂得。
ryfdizuo 2011-09-01
  • 打赏
  • 举报
回复


char* find_max_sub_str(const char* str)
{
const char* pstr = NULL;
int max_length = 0;

const char *p = str;

int count[256] = {0};
int length = 0;

while (*p)
{
if ( count[*p]==0 )
{
count[ *p ]++;
++length;

++p;
}
else
{
if (length > max_length)
{
max_length = length;
pstr = p - length;
}

// reset
p = p - length + 1;
memset(count, 0, sizeof(count));
length = 0;
}
}

if (length > max_length)
{
max_length = length;
pstr = p - length;
}

char* ret = new char[max_length+1];
strncpy(ret, pstr, max_length);
ret[max_length] = '\0';

return ret;
}

int main()
{
{
char buf[] = "axdbxce";
char* dest = find_max_sub_str(buf);
cout << dest << endl;
delete[] dest;
}

{
char buf[] = "axdbx";
char* dest = find_max_sub_str(buf);
cout << dest << endl;
delete[] dest;
}

system("PAUSE");

return 0;
}

dbxce
axdb
Press any key to continue . . .

woshiqian84425 2011-09-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 merlin_q 的回复:]
题目:编写一个函数int search(char *text),text为输入的字符串,从字符串中找出一个最长的不含重复字符的子字符串,例如“axdbx”,返回4,子字符串为“axdb”,而“axdbxce”,返回5,子字符串为“dbxce”。时间复杂度为o(n).
[/Quote]
“axdbxce”,返回5,子字符串为“dbxce”。是什么意思,我看题目的意思是子字符串为“axdbce”啊;
chenyu347 2011-09-01
  • 打赏
  • 举报
回复
坐等高手
liveeng 2011-09-01
  • 打赏
  • 举报
回复
面试题 == 作业题
ringer564597 2011-09-01
  • 打赏
  • 举报
回复
哪的面试题,觉得最近的面试题都很不靠谱。
無_1024 2011-09-01
  • 打赏
  • 举报
回复
写一个思路 用字母 - 'a'作为数组下标来存储 一次循环 当相应下标的数组的值大于1时 说明有重复的用一个变量计数就可以了
龙哥依旧 2011-09-01
  • 打赏
  • 举报
回复
帮顶!
:)

33,321

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧