mirror of
https://github.com/This-weiyuqing/DSAndOS.git
synced 2026-10-18 03:03:58 +08:00
update
This commit is contained in:
1 parent
16d694ffe3
commit
35e5484d7c
183 files changed
+362
-7145
No files matched your search
@@ -0,0 +1,60 @@
|
||||
/***字符串匹配算法***/
|
||||
#include<cstring>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
#define OK 1
|
||||
#define ERROR 0
|
||||
#define OVERFLOW -2
|
||||
typedef int Status;
|
||||
#define MAXSTRLEN 255 //用户可在255以内定义最长串长
|
||||
typedef char SString[MAXSTRLEN+1]; //0号单元存放串的长度
|
||||
|
||||
Status StrAssign(SString T, char *chars) { //生成一个其值等于chars的串T
|
||||
int i;
|
||||
if (strlen(chars) > MAXSTRLEN)
|
||||
return ERROR;
|
||||
else {
|
||||
T[0] = strlen(chars);
|
||||
for (i = 1; i <= T[0]; i++)
|
||||
T[i] = *(chars + i - 1);
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
//算法4.1 BF算法
|
||||
int Index(SString S, SString T, int pos)
|
||||
{
|
||||
//返回模式T在主串S中第pos个字符之后第s一次出现的位置。若不存在,则返回值为0
|
||||
//其中,T非空,1≤pos≤StrLength(S)
|
||||
int i = pos;
|
||||
int j = 1;
|
||||
while(i <= S[0]&&j <= T[0])
|
||||
{
|
||||
if(S[i]==T[j])
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
} //继续比较后继字符
|
||||
else
|
||||
{
|
||||
i=i-j+2;
|
||||
j=1;
|
||||
} //指针后退重新开始匹配
|
||||
}
|
||||
if (j > T[0])
|
||||
return i - T[0];
|
||||
else
|
||||
return 0;
|
||||
return 0;
|
||||
}//Index
|
||||
|
||||
int main()
|
||||
{
|
||||
SString S;
|
||||
StrAssign(S,"bbaaabbaba") ;
|
||||
SString T;
|
||||
StrAssign(T,"abb") ;
|
||||
cout<<"主串和子串在第"<<Index(S,T,1)<<"个字符处首次匹配\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/***字符串匹配算法***/
|
||||
#include<cstring>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
#define OK 1
|
||||
#define ERROR 0
|
||||
#define OVERFLOW -2
|
||||
typedef int Status;
|
||||
#define MAXSTRLEN 255 //用户可在255以内定义最长串长
|
||||
typedef char SString[MAXSTRLEN+1]; //0号单元存放串的长度
|
||||
|
||||
Status StrAssign(SString T, char *chars) { //生成一个其值等于chars的串T
|
||||
int i;
|
||||
if (strlen(chars) > MAXSTRLEN)
|
||||
return ERROR;
|
||||
else {
|
||||
T[0] = strlen(chars);
|
||||
for (i = 1; i <= T[0]; i++)
|
||||
T[i] = *(chars + i - 1);
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
//算法4.3 计算next函数值
|
||||
void get_next(SString T, int next[])
|
||||
{ //求模式串T的next函数值并存入数组next
|
||||
int i = 1, j = 0;
|
||||
next[1] = 0;
|
||||
while (i < T[0])
|
||||
if (j == 0 || T[i] == T[j])
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
next[i] = j;
|
||||
}
|
||||
else
|
||||
j = next[j];
|
||||
}//get_next
|
||||
|
||||
//算法4.2 KMP算法
|
||||
int Index_KMP(SString S, SString T, int pos, int next[])
|
||||
{ // 利用模式串T的next函数求T在主串S中第pos个字符之后的位置的KMP算法
|
||||
//其中,T非空,1≤pos≤StrLength(S)
|
||||
int i = pos, j = 1;
|
||||
while (i <= S[0] && j <= T[0])
|
||||
if (j == 0 || S[i] == T[j]) // 继续比较后继字
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
}
|
||||
else
|
||||
j = next[j]; // 模式串向右移动
|
||||
if (j > T[0]) // 匹配成功
|
||||
return i - T[0];
|
||||
else
|
||||
return 0;
|
||||
}//Index_KMP
|
||||
|
||||
int main()
|
||||
{
|
||||
SString S;
|
||||
StrAssign(S,"aaabbaba") ;
|
||||
SString T;
|
||||
StrAssign(T,"abb") ;
|
||||
int *p = new int[T[0]+1]; // 生成T的next数组
|
||||
get_next(T,p);
|
||||
cout<<"主串和子串在第"<<Index_KMP(S,T,1,p)<<"个字符处首次匹配\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
/***字符串匹配算法***/
|
||||
#include<cstring>
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
#define OK 1
|
||||
#define ERROR 0
|
||||
#define OVERFLOW -2
|
||||
typedef int Status;
|
||||
#define MAXSTRLEN 255 //用户可在255以内定义最长串长
|
||||
typedef char SString[MAXSTRLEN+1]; //0号单元存放串的长度
|
||||
|
||||
Status StrAssign(SString T, char *chars) { //生成一个其值等于chars的串T
|
||||
int i;
|
||||
if (strlen(chars) > MAXSTRLEN)
|
||||
return ERROR;
|
||||
else {
|
||||
T[0] = strlen(chars);
|
||||
for (i = 1; i <= T[0]; i++)
|
||||
T[i] = *(chars + i - 1);
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
//算法4.4 计算next函数修正值
|
||||
void get_nextval(SString T, int nextval[])
|
||||
{ // 求模式串T的next函数修正值并存入数组nextval
|
||||
int i = 1, j = 0;
|
||||
nextval[1] = 0;
|
||||
while (i < T[0])
|
||||
if (j == 0 || T[i] == T[j])
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
if (T[i] != T[j])
|
||||
nextval[i] = j;
|
||||
else
|
||||
nextval[i] = nextval[j];
|
||||
} else
|
||||
j = nextval[j];
|
||||
}//get_nextval
|
||||
|
||||
//算法4.2 KMP算法
|
||||
int Index_KMP(SString S, SString T, int pos, int next[])
|
||||
{ // 利用模式串T的next函数求T在主串S中第pos个字符之后的位置的KMP算法
|
||||
//其中,T非空,1≤pos≤StrLength(S)
|
||||
int i = pos, j = 1;
|
||||
while (i <= S[0] && j <= T[0])
|
||||
if (j == 0 || S[i] == T[j]) // 继续比较后继字
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
}
|
||||
else
|
||||
j = next[j]; // 模式串向右移动
|
||||
if (j > T[0]) // 匹配成功
|
||||
return i - T[0];
|
||||
else
|
||||
return 0;
|
||||
}//Index_KMP
|
||||
|
||||
int main()
|
||||
{
|
||||
SString S;
|
||||
StrAssign(S,"babaaaabbaba") ;
|
||||
SString T;
|
||||
StrAssign(T,"abb") ;
|
||||
int *p = new int[T[0]+1]; // 生成T的next数组
|
||||
get_nextval(T,p);
|
||||
cout<<"主串和子串在第"<<Index_KMP(S,T,1,p)<<"个字符处首次匹配\n";
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
#include<iostream>
|
||||
#include<fstream>
|
||||
#include<string.h>
|
||||
using namespace std;
|
||||
|
||||
typedef struct{
|
||||
char ch[600]; //若是非空串,则按串长分配存储区,否则ch为NULL
|
||||
int len; //串长度
|
||||
}HString;
|
||||
|
||||
|
||||
int Index_BF(HString S,HString T,int pos)
|
||||
{//返回模式T在主串S中第pos个字符开始第一次出现的位置。若不存在,则返回值为0
|
||||
//其中,T非空,1≤pos≤StrLength(S)
|
||||
int i,j;
|
||||
i=pos; j=1;
|
||||
while(i<=S.len&&j<=T.len)
|
||||
{
|
||||
if(S.ch[i]==T.ch[j]){++i;++j;} //继续比较后继字符
|
||||
else{i=i-j+2;j=1;} //指针后退重新开始匹配
|
||||
}
|
||||
if(j>T.len) return i-T.len;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
void Virus_detection()
|
||||
{
|
||||
int num,m,flag,i,j; char Vir[600];
|
||||
HString Virus,Person,temp;
|
||||
ifstream inFile("病毒感染检测输入数据.txt");
|
||||
ofstream outFile("病毒感染检测输出结果.txt");
|
||||
inFile>>num;//读取待检测的任务数
|
||||
while(num--) //依次检测每对病毒DNA和人的DNA是否匹配
|
||||
{
|
||||
inFile>>Virus.ch+1;//读取病毒DNA序列
|
||||
inFile>>Person.ch+1;//读取人的DNA序列
|
||||
strcpy(Vir,Virus.ch);
|
||||
Virus.len=strlen(Virus.ch)-1;
|
||||
Person.len=strlen(Person.ch)-1;
|
||||
flag=0;//用来标识是否匹配,初始为0,匹配后为非0
|
||||
m=Virus.len;
|
||||
for(i=m+1,j=1;j<=m;j++) Virus.ch[i++]=Virus.ch[j];
|
||||
//因病毒为环状,故将病毒的长度扩大2倍
|
||||
//即可线性取到所有长度为Virus.len的字符串
|
||||
Virus.ch[2*m+1]='\0'; //添加结束符号
|
||||
for(i=0;i<m;i++)
|
||||
{
|
||||
for(j=1;j<=m;j++) temp.ch[j]=Virus.ch[i+j];
|
||||
//取长为Virus.len的环形字符串
|
||||
//即Virus.ch[0]-- Virus.ch[Virus.len-1],
|
||||
//Virus.ch[1]-- Virus.ch[0], Virus.ch[2]-- Virus.ch[1]...
|
||||
temp.ch[m+1]='\0'; //添加结束符号
|
||||
temp.len=strlen(temp.ch)-1;
|
||||
flag=Index_BF(Person,temp,1); //模式匹配
|
||||
if(flag) break; //匹配即可退出循环
|
||||
}//for
|
||||
if(flag)
|
||||
outFile<<Vir+1<<" "<<Person.ch+1<<" "<<"YES"<<endl;
|
||||
else
|
||||
outFile<<Vir+1<<" "<<Person.ch+1<<" "<<"NO"<<endl;
|
||||
}//while
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
Virus_detection();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
11
|
||||
baa bbaabbba
|
||||
baa aaabbbba
|
||||
aabb abceaabb
|
||||
aabb abaabcea
|
||||
abcd cdabbbab
|
||||
abcd cabbbbab
|
||||
abcde bcdedbda
|
||||
acc bdedbcda
|
||||
cde cdcdcdec
|
||||
cced cdccdcce
|
||||
bcd aabccdxdxbxa
|
||||
Reference in new issue
Block a user