给一个字符串、例如 “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:if test="${not empty dataValue}">
fm.SAMPLING_DATE.value=" <c:out value='${dataValue.SAMPLING_DATE}'/ ......
问题描述:
有一简单的C程序,如下:
void foo ( ) {
int i ;
p r i n t f ( "%d " , i ++);
}
int main ( ){ int j ;
for ( j = 0 ; j < 1 0 ; ++j ) foo ( ) ;
}
函 ......
c/s 和b/s 的区别是什么,c/s需要服务器吗,怎么判断程序是c/s 还是b/s
C/S是服务器和客户端 B/S是服务器和浏览器
他们都有s,什么是s?就是server
那他们的区别是什么?一个是c,即client,一个 ......
struct s1 {
char ch, *ptr;
union {
short a, b;
unsigned int c:2, d:1;
}
struct s1 *next;
};
主要看不懂符号 :
请达人指点一二
http://blog.cechina.cn/true ......
开始学OS,按练习要求写的代码在gcc下无法编译:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
void SIGFPEhandler2(int s ......