?? 最大子串匹配.txt
字號:
//最大子串匹配,復(fù)雜度O(mn)
//返回最大匹配值,傳入兩個串和串的長度,重載返回一個最大匹配
//注意做字符串匹配是串末的'\0'沒有置!
//可更改元素類型,更換匹配函數(shù)和匹配價值函數(shù)
#include <string.h>
#define MAXN 100
#define max(a,b) ((a)>(b)?(a):(b))
#define _match(a,b) ((a)==(b))
#define _value(a,b) 1
typedef char elem_t;
int str_match(int m,elem_t* a,int n,elem_t* b){
int match[MAXN+1][MAXN+1],i,j;
memset(match,0,sizeof(match));
for (i=0;i<m;i++)
for (j=0;j<n;j++)
match[i+1][j+1]=max(max(match[i][j+1],match[i+1][j]),
(match[i][j]+_value(a[i],b[i]))*_match(a[i],b[j]));
return match[m][n];
}
int str_match(int m,elem_t* a,int n,elem_t* b,elem_t* ret){
int match[MAXN+1][MAXN+1],last[MAXN+1][MAXN+1],i,j,t;
memset(match,0,sizeof(match));
for (i=0;i<m;i++)
for (j=0;j<n;j++){
match[i+1][j+1]=(match[i][j+1]>match[i+1][j]?match[i][j+1]:match[i+1][j]);
last[i+1][j+1]=(match[i][j+1]>match[i+1][j]?3:1);
if ((t=(match[i][j]+_value(a[i],b[i]))*_match(a[i],b[j]))>match[i+1][j+1])
match[i+1][j+1]=t,last[i+1][j+1]=2;
}
for (;match[i][j];i-=(last[t=i][j]>1),j-=(last[t][j]<3))
ret[match[i][j]-1]=(last[i][j]<3?a[i-1]:b[j-1]);
return match[m][n];
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -