幾個最大子字串的演算法題

2021-09-09 03:49:13 字數 3060 閱讀 5882

統計乙個字串中所有字元出現的次數

基本思路:建立乙個訪問標誌陣列,初始化為訪問次數0,每訪問一次,將其增1:

static int count[128];

遍歷字串陣列,每次讀取乙個字元ch; count[ch]++; 這樣只要遍歷一次陣列就行了。在最後把count[i]==0的去掉即可

#include

using namespace std;

int main()

int count[255] =;

char *pstrtemp = "243567867867867";

docount[*pstrtemp]++;

}while (*(++pstrtemp));

for(int i = 0; i < 255; ++i)

if (count[i] != 0)

cout << "character:" << (char)i <<" number:" << count[i] << endl;

求最大的相同字元子串(由同乙個字元組成)

不處理相同最大字串的情況,可處理僅乙個字元的情況。求出每個字母對應的最長字串

預設為只處理大寫字母,否則可以將26更改為128即可

void ddstrsame(char * ch)

int tempch[26];

int i,maxlen =1,count=1;

char curchar;

for(i=0;i<26;i++) // 將每個字元的初始訪問次數置為0

tempch[i] = 0;

//第乙個應單獨處理,防止下面越界

tempch[*ch-0x61]++;

curchar = *ch++; // 若字串只有乙個字元時,此時很重要

while(*ch != '/0')

// 與上乙個相同時,增加計數1,若不同,則更新當前字元的最大訪問次數

if(*ch == *(ch-1)) //

count++;

else

if( count > tempch[*(ch-1)-0x61])

tempch[*(ch-1)-0x61] = count;

if(maxlen < count) // 當前字元的最大計數大於所有字元中的最大計數,則/更新最大計數及對應的字元

maxlen = count;

curchar = *(ch-1);

count = 1; // 以便對下乙個字元計數

ch++;

if( count > tempch[*(ch-1)-0x61])

tempch[*(ch-1)-0x61] = count;

if(maxlen < count) // 當前字元的最大計數大於所有字元中的最大計數,則/更新最大計數及對應的字元

maxlen = count;

curchar = *(ch-1);

printf("character = %c/tnumber = %d/n",curchar,maxlen);

void main()

ddstrsame("b");

ddstrsame("abcdddddaaaaabbbeefffda");

ddstrsame("aabcdddaaaaabbbeefffda");

ddstrsame("aaaaaabcdddaaaaabbbeefffda");

ddstrsame("aaabcdddaaaaabbbeefffdaaaaaa");

ddstrsame("aaabcdddaaaaabbbeeffffffffffffdaaaaaab");

system("pause");

寫乙個函式,它的原形是int findmaxintstr(char *outputstr,char *intputstr)

功能:在字串中找出連續最長的數字串,並把這個串的長度返回,並把這個最長數字串付給其中乙個函式引數outputstr所指記憶體。例如:"abcd12345ed125ss123456789"的首位址傳給intputstr後,函式將返回9,outputstr所指的值為123456789

// 不處理含相同長度的最長子串,若有,則返回的是第一次出現的最長字串

int findmaxintstr (char *outputstr, char *inputstr)

char *in = inputstr, *out = outputstr, *temp, *final;

int count = 0, maxlen = 0;

while( *in != '/0' )

if( *in >= 『0』 && *in <= 『9』 )

for(temp = in; *in >= 『0』 && *in <= 『9』; in++ )

count++;

if( maxlen < count )

maxlen = count;

//count = 0;

final = temp; // temp儲存了當前連續最長字串的首位址,final是總的最長

*(final+count) = 『/0』; // 給字串賦上結束符

count = 0; // 不管當前累計的最長數字是否大於前面最長的,都應該清除count,以便下/次計數

//else 不管上面的if是否進入,到此處時*in肯定不是數字

in++;

// 上述比較過程只儲存了最長字串在輸入陣列中對應的位址,避免了反覆拷貝到輸出陣列/的過程

for(int i = 0; i < maxlen; i++) // 將最終的最長字串儲存到輸出陣列

*out++ = *final++;

*out = '/0';

return maxlen;

int main()

char input = "abcd12345ed125ss0123456789";

char output[50] = ;

unsigned int maxlength = findmaxintstr (output, input);

cout << output

幾個最大子字串的演算法題

幾個最大子字串的演算法題 統計乙個字串中所有字元出現的次數 基本思路 建立乙個訪問標誌陣列,初始化為訪問次數0,每訪問一次,將其增1 static int count 128 遍歷字串陣列,每次讀取乙個字元ch count ch 這樣只要遍歷一次陣列就行了。在最後把count i 0的去掉即可 in...

演算法題之 最大子串

題目 給定一字串只包含數字,請寫乙個演算法,找出該字串中的最長不重複子串 不重複是指子串中每一元素不同於子串中其他元素 如 120135435 最長不重複子串為 201354 方法一 輔助陣列,o n n private static string norepeatsubstring string ...

最大子串演算法

最大子串問題是一類經典問題,即在一串整形陣列中選取和最大的子串 給出問題描述 對於乙個包含負值的數字串array 1.n 要找到他的乙個子串array i.j 0 i j n 使得在array的所有子串中,array i.j 的和最大。針對本問題,可有三種方法,一種是暴利破解列舉演算法,所有子串種類...