易截截图软件、单文件、免安装、纯绿色、仅160KB

关于字符串的C面试题

给一个字符串、例如 “ababc”要求返回“ab”. 因为“ab”连续重复出现且最长。  用C/C++语言写一函数完成该算法,给出复杂度
这个题我不会

C/C++ code:

#include <iostream>
#include <string>

using namespace std;

bool getmaxsubstr(const string& s, string& oks);

int main()
{
string str;

while (cin >> str)
{
string s;
if (getmaxsubstr(str, s))
{
cout << s << endl;
}
else
{
cout << "do not exist!\n";
}
}

return 0;
}

bool getmaxsubstr(const string& s, string& oks)
{
string rets;
int len = 0;
bool bf = false;
for (int i = 0; i < s.size(); ++i)
{
for (int j = 1; j <= s.size() - i; ++j)
{
rets = s.substr(i, j);
if (s.find(rets, i + j) != string::npos && rets.size() > len)
{
len = rets.size();
oks = rets;
bf = true;
}
}
}

return bf;
}


O(n^2),没有考虑substr内部的复杂度。

问题要求能否再清晰一些

比如:"aababcabcd"的情况下,ab出现了3次,而abc出现了2次,应该返回哪一个?

#include <stdio.h>

#define BUF_MAX 1000


相关问答:

c基础

#include <string.h>
#include <stdio.h>

void main()
{
int i;
char buf[]="abcde";
strncpy(buf,"abc",3);
for(i=0;i <5;i++)
printf(&q ......

c LINUX 问题

GCC是免费的吗?那有?

LINUX系统怎么装?


中文怎么设置?
你装完Linux系统就会自带GCC,windows下也有GCC,比如dev-c++,CODE::Blocks
linux系统安装根据不同的linux版本网上都有教程,看下就 ......

这个是什么意思,unsigned int c:2, d:1;

struct s1 {
char ch, *ptr;
union {
short a, b;
unsigned int c:2, d:1;
}
struct s1 *next;
};


主要看不懂符号 :

请达人指点一二
http://blog.cechina.cn/true ......

extern "C"问题

/*-----------c.h--------------*/
#ifndef _C_H_
#define _C_H_
extern "C" int add(int x, int y);
#endif

/*-----------c.c--------------*/
int add(int x, int y){
return ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号