给一个字符串、例如 “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
相关问答:
有这样两个问题,希望高手指点:
第一:
struct struct_A{
int a;
char b;
int c;
short d;
}
struct struct_B{
int a;
char b;
short c;
......
各位大侠,小弟有一个问题请教,如下
这个 #define stack_push 是一个函数,调用是这样的
int len;
stack_t *stack;
const char *curpos;
int seglen;
const char *next_slash;
int i;
......
散分,为C/C++论坛增加点人气!!
jf
jf,楼下保持队形
只见过不敢散分的,没见过不敢接分的
接
前排留名
jf
这里人气还不错啊!
接分 …… C/C++好热闹额……其他版块就相对没这么热了
......
我现在想将这四个文件从服务器\\10.2.95.88\temp目录下的四个文本文件:1.txt ,2.txt ,3.txt , 4.txt从服务器下载到本地机的C:\temp目录下?
上述的功能我想用纯C应该如何实现?
用ftp协议就可以了
引用 ......