给一个字符串、例如 “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
相关问答:
13个人围成一圈,从第一个人开始顺序报号1,2,3。凡报到3者退出圈子,找出最后留在圈子中的人原来的序号
结果应该是13 可我的程序的结果是11 希望好心人帮改一下
#include <stdio.h>
#include < ......
有这样两个问题,希望高手指点:
第一:
struct struct_A{
int a;
char b;
int c;
short d;
}
struct struct_B{
int a;
char b;
short c;
......
struct s1 {
char ch, *ptr;
union {
short a, b;
unsigned int c:2, d:1;
}
struct s1 *next;
};
主要看不懂符号 :
请达人指点一二
http://blog.cechina.cn/true ......
/*-----------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 ......