首页 > 代码库 > 随意输入N个英文字符,找出其中最长连续的排列。
随意输入N个英文字符,找出其中最长连续的排列。
int out_max_length_crease_str(const char *p, std::vector<std::string> &vct)
{
vct.clear();
int nlen = strlen(p);
if (nlen == 0){
return 0;
}
if (nlen == 1){
vct.push_back(p);
return 1;
}
char *buf = new char(nlen);
memset(buf, 0, nlen);
char first_char = *p;
char *pbuf = buf;
while (*++p != 0){
if (*p > first_char){
*pbuf++ = *p;
}
}
if (pbuf == buf){
std::string s;
s += first_char;
vct.push_back(s);
return 1;
}
int nret = 0;
std::vector<std::string> vct_temp;
const char *temp = buf;
for (; *temp != 0; ++temp){
int ntemp = out_max_length_crease_str(temp, vct_temp);
if (ntemp > nret){
nret = ntemp;
vct.swap(vct_temp);
}
else if (ntemp == nret){
vct.insert(vct.end(), vct_temp.begin(), vct_temp.end());
}
}
std::string s;
for (std::string &str : vct){
str.insert(str.begin(), first_char);
}
return nret + 1;
}
int out_max_length_crease_str_ex(const char *p, std::vector<std::string> &vct)
{
int max_lenth = 0;
std::vector<std::string> vct_temp;
const char *temp = p;
for (; *temp != 0; ++temp){
int lenth = out_max_length_crease_str(temp, vct_temp);
if (lenth > max_lenth){
max_lenth = lenth;
vct.swap(vct_temp);
}
else if (lenth == max_lenth){
vct.insert(vct.end(), vct_temp.begin(), vct_temp.end());
}
}
return max_lenth;
}
int n = out_max_length_crease_str_ex("etphdumei", vct);
n = out_max_length_crease_str_ex("vfleqiynp", vct);
n = out_max_length_crease_str_ex("abdfghimo", vct);
n = out_max_length_crease_str_ex("QMWBZGZGE", vct);
只不过目前没有去重,最后一个例子,会返回几个重复的。
int out_max_length_crease_str(const char *p, std::vector<std::string> &vct)
{
vct.clear();
int nlen = strlen(p);
if (nlen == 0){
return 0;
}
if (nlen == 1){
vct.push_back(p);
return 1;
}
char *buf = new char[nlen];
memset(buf, 0, nlen);
char first_char = *p;
char *pbuf = buf;
while (*++p != 0){
if (*p > first_char){
*pbuf++ = *p;
}
}
if (pbuf == buf){
std::string s;
s += first_char;
vct.push_back(s);
return 1;
}
int nret = 0;
std::vector<std::string> vct_temp;
pbuf = buf;
for (; *pbuf != 0; ++pbuf){
int ntemp = out_max_length_crease_str(pbuf, vct_temp);
if (ntemp > nret){
nret = ntemp;
vct.swap(vct_temp);
}
else if (ntemp == nret){
vct.insert(vct.end(), vct_temp.begin(), vct_temp.end());
}
}
delete []buf;
for (std::string &str : vct){
str.insert(str.begin(), first_char);
}
return nret + 1;
}
{
vct.clear();
int nlen = strlen(p);
if (nlen == 0){
return 0;
}
if (nlen == 1){
vct.push_back(p);
return 1;
}
char *buf = new char(nlen);
memset(buf, 0, nlen);
char first_char = *p;
char *pbuf = buf;
while (*++p != 0){
if (*p > first_char){
*pbuf++ = *p;
}
}
if (pbuf == buf){
std::string s;
s += first_char;
vct.push_back(s);
return 1;
}
int nret = 0;
std::vector<std::string> vct_temp;
const char *temp = buf;
for (; *temp != 0; ++temp){
int ntemp = out_max_length_crease_str(temp, vct_temp);
if (ntemp > nret){
nret = ntemp;
vct.swap(vct_temp);
}
else if (ntemp == nret){
vct.insert(vct.end(), vct_temp.begin(), vct_temp.end());
}
}
std::string s;
for (std::string &str : vct){
str.insert(str.begin(), first_char);
}
return nret + 1;
}
int out_max_length_crease_str_ex(const char *p, std::vector<std::string> &vct)
{
int max_lenth = 0;
std::vector<std::string> vct_temp;
const char *temp = p;
for (; *temp != 0; ++temp){
int lenth = out_max_length_crease_str(temp, vct_temp);
if (lenth > max_lenth){
max_lenth = lenth;
vct.swap(vct_temp);
}
else if (lenth == max_lenth){
vct.insert(vct.end(), vct_temp.begin(), vct_temp.end());
}
}
return max_lenth;
}
int n = out_max_length_crease_str_ex("etphdumei", vct);
n = out_max_length_crease_str_ex("vfleqiynp", vct);
n = out_max_length_crease_str_ex("abdfghimo", vct);
n = out_max_length_crease_str_ex("QMWBZGZGE", vct);
只不过目前没有去重,最后一个例子,会返回几个重复的。
int out_max_length_crease_str(const char *p, std::vector<std::string> &vct)
{
vct.clear();
int nlen = strlen(p);
if (nlen == 0){
return 0;
}
if (nlen == 1){
vct.push_back(p);
return 1;
}
char *buf = new char[nlen];
memset(buf, 0, nlen);
char first_char = *p;
char *pbuf = buf;
while (*++p != 0){
if (*p > first_char){
*pbuf++ = *p;
}
}
if (pbuf == buf){
std::string s;
s += first_char;
vct.push_back(s);
return 1;
}
int nret = 0;
std::vector<std::string> vct_temp;
pbuf = buf;
for (; *pbuf != 0; ++pbuf){
int ntemp = out_max_length_crease_str(pbuf, vct_temp);
if (ntemp > nret){
nret = ntemp;
vct.swap(vct_temp);
}
else if (ntemp == nret){
vct.insert(vct.end(), vct_temp.begin(), vct_temp.end());
}
}
delete []buf;
for (std::string &str : vct){
str.insert(str.begin(), first_char);
}
return nret + 1;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。