算法,寻找单一子串

25 篇文章 0 订阅

从一个长串中找出没有相同字符的最长子串。

int find_length(std::string temp,char c)
{
    printf("temp %s,%c.\n",temp.c_str(),c);
    int length = temp.length();
    for(int i = 0;i<temp.length();i++)
    {
        if(temp[i] == c)
        {
            return temp.length();
            if(temp.length() - i > i)
            {
                return temp.length() - i;
            }
            else
            {
                return i;
            }
        }
    }
    return -1;
}

int GetLongestSignelString(std::string value)
{
    std::string temp;
    int max_count = 0;
    for(int i = 0;i<value.length();i++)
    for(int j = i + 1;j<value.length();j++)
    {
        temp = value.substr(i,j-i);
        int found_len = find_length(temp,value[j]);
        printf(" found_len %d.############ i %d j %d len %d\n",found_len,i,j,temp.length());
        if(found_len > 0)
        {
            if(found_len > max_count)
            {
                max_count = found_len;
            }
            printf("break 11.max_count %d\n",max_count);
            break;
        }
        else
        {
            if(j == value.length() - 1)
            {
                if(temp.length() + 1 > max_count)
                {
                    max_count = temp.length() + 1;
                }
                printf("break here.max_count %d\n",max_count);
                break;
            }
            printf("break 22.\n");
        }
    }
    return max_count;
}

int main()
{
    std::string str("accbjhbsaalkbsdwie12324326492!@#%$&^*VMYHG");
    printf("longest %d.\n",GetLongestSignelString(str));
    return 0;
}
最近优化了一个高效一些的版本。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>

int findLast(char* a, int len, char c)
{
    if (len == 0) {
        return 0;
    }
    for (int i = len - 1; i >= 0;i --)
    if (a[i] == c) {
        return i + 1;
    }
    return 0;
}

std::string SingleStr(const std::string& str)
{
    int validlen = 0;
    std::string result;
    char *bufstr = (char*)malloc(str.length());
    for (int i = 0; i<str.length(); i++)     
    {
        int find = findLast(bufstr, validlen, str[i]);
        if (find != 0) {
            if (find > result.length()) {
                result.assign(bufstr, find);
            } 
            memmove(bufstr, bufstr + find, validlen - find);
            validlen -= find;
        } 
        bufstr[validlen ++] = str[i];
        if (validlen > result.length()) {
            result.assign(bufstr, validlen);
        }
    }
    if (validlen > result.length()) {
        result.assign(bufstr, validlen);
    }
    free(bufstr);
    return result;
}


int main()
{
    std::string a("120135435");
    std::cout<<SingleStr(a)<<std::endl;
}
 

以上使用的是蛮力比的过程输出,不过效率不高,比较耗时

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值