易截截图软件、单文件、免安装、纯绿色、仅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的问题哈

C/C++ code:
#include <stdio.h>
typedef struct node{
int *key;
int length;
}node;
void insert(node &temp,int n)
{
int i,j;
for(i=0;i<=temp.length;i++)
{
......

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版本网上都有教程,看下就 ......

请教c高手,define的问题

各位大侠,小弟有一个问题请教,如下
这个 #define stack_push 是一个函数,调用是这样的

int len;
stack_t *stack;
const char *curpos;
int seglen;
const char *next_slash;
int i;
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号