兩個字串中最長公共單詞 C語言

2021-09-27 04:28:27 字數 1053 閱讀 2352

請僅編寫出一c語言函式char *maxword(char *s, char *t),該函式的功能是求出字串s與字串t的最長公共單詞(這裡,假設兩個字串均由英文本母和空格字元組成);若找到這樣的公共單詞,函式返回該單詞,否則,函式返回null。

例如:若s=「this is c programming text」,t=「this is a text for c programming」,則函式返回「programming」。

要求:1. 函式中不得設定儲存單詞的儲存空間;

2. 給出函式之前請用文字簡要敘述函式的基本思想。

#include #include #include int getmaxlengthstr(char *source,int index,int length,char *target)

pre=i+1;

}} return len;

}char * maxword(char *s,char *t)

}// 將單詞的起始位置置為空格的後一位

pre=i+1;

}} if(length>0)else }

int main(int argc, char *ar** )

➜  c gcc temp.c

➜ c ./a.out "this is c programming text" "this is a text for c programming"

programming

➜ c ./a.out "this is c programming text" "this is damon"

this

➜ c ./a.out "this is c programming text" "damon is cool"

is➜ c ./a.out "this is c programming text" "cool"

c➜ c ./a.out "this is c programming text" "damon"

(null)

➜ c

請多指教

求兩個字串最長公共字串

package com.test 求兩個字串最長公共字串,演算法 兩個字串形成乙個矩陣,將兩個字元不匹配的位置標記為0,c i j 中兩個字元相匹配的位置標記為n,其中n c i 1 j 1 1,其中沿正對角線方向最長的序列為兩個字串的最長公共子串行 public class lcs public ...

java求兩個字串最長公共字串

思路 將str1的字元用兩個for迴圈用substring i,j 逐段截出,再與str2內字串比較,從而選出最長公共的字串,從而輸出字元內串 public class similarstring for int i 0 i str1.length 1 i system.out.println 兩字...

兩個字串的最長公共子串行

解題分析 設兩個序列x y 用lsc x,y 表示最長公共子串行。1.如果xn yn。則該元素一定存在於公共子串行中,所以可化為尋求子問題lsc x 1,y 1 用公式可表示為lsc x,y lsc x 1,y 1 1。2.如果xn yn。則該元素不存在於公共子串行中,所以轉而去尋求兩個子問題,即l...