A | 字符串变换 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
string a,b;
int n,m,k=0;
cin>>n>>m;
cin>>a>>b;
for(int i=0;i<min(m,n);i++){
if(a[i]!=b[i]) k++;
}
cout<<k+abs(m-n);
return 0;
}
B | 字符串求反 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
string s;
cin>>s;
reverse(s.begin(),s.end());
cout<<s.length()<<endl;
cout<<s;
return 0;
}
D | 字符串匹配(朴素算法)-附加代码模式 |
#include<bits/stdc++.h>
using namespace std;
#define max 20000000
int findPos(char s[],char t[]){
int m=strlen(s),n=strlen(t);
int i=0,j=0;
while(i<m&&j<n){
if(s[i]==t[j]){
i++,j++;
}
else{
i=i-j+1;
j=0;
}
}
if(j>=n) return i-j;
else return -1;
}
E | 求解最长首尾公共子串-附加代码模式 |
#include<bits/stdc++.h>
using namespace std;
#define max 100
int nex[max+7];
int calcLCT(char t[]){
if(strlen(t)==1) return -1;
nex[0]=-1,nex[1]=0;
for(int i=1,j=-1;i<strlen(t);i++){
while(j!=-1&&t[i]!=t[j+1]) j=nex[j];
if(t[i]==t[j+1]) j++;
nex[i]=j;
}
int len=-1;
for(int i=0;i<strlen(t);i++)
if(len<nex[i]) len=nex[i];
return len+1;
}
F | 算法4-7:KMP算法中的模式串移动数组-附加代码模式 |
#include<bits/stdc++.h>
using namespace std;
#define max 100
//int nex[max+7];
void getNext(char t[],int nex[]){
//if(strlen(t)==1) return -1;
nex[0]=-1,nex[1]=0;
for(int i=2;i<strlen(t);i++){
int k=nex[i-1];
while(k>=0&&t[k]!=t[i-1]) k=nex[k];
//if(t[i]==t[j+1]) j++;
nex[i]=k+1;
}
//int len=-1;
//for(int i=0;i<strlen(t);i++)
// if(len<nex[i]) len=nex[i];
//return len+1;
}
/*int main()
{
char t[max];
cin>>t;
int len=strlen(t);
int next[len];
getNext(t,next);
for(int i=0;i<len;i++) cout<<next[i]<<" ";
return 0;
}*/
G | 数据结构作业03 -- 改进的nextVal向量 |
#include<bits/stdc++.h>
using namespace std;
#define max 300
//int nex[max+7];
void getNext(char t[],int nex[]){
//if(strlen(t)==1) return -1;
nex[0]=-1,nex[1]=0;
int i=0,j=-1;
while(i<strlen(t)){
if(j==-1||t[i]==t[j]){
i++,j++;
if(t[i]!=t[j]) nex[i]=j;
else nex[i]=nex[j];
}
else j=nex[j];
}
//int len=-1;
//for(int i=0;i<strlen(t);i++)
// if(len<nex[i]) len=nex[i];
//return len+1;
}
int main()
{
char t[max];
while(cin>>t){
int len=strlen(t);
int next[len];
getNext(t,next);
for(int i=0;i<len;i++) cout<<next[i]<<" ";
cout<<endl;
}
return 0;
}
H | 算法4-6:字符串匹配算法执行次数比较(朴素、KMP、改进的KMP)-附加代码模式 |
#include<bits/stdc++.h>
using namespace std;
#define max 300
int findPos(char s[], char t[]){
int count=0;
int m=strlen(s), n= strlen(t);
int i=0,j=0;
while(i<m && j<n){
count++;
if(s[i]==t[j]) i++,j++;
else{
i=i-j+1;
j=0;
}
}
cout<<"count in findPos is:"<<count<<endl;
if(j>=n) return i-j;
else return -1;
}
// calculate the next array for KMP algorithm
void CalcNext(char t[],int nex[]){
nex[0]=-1,nex[1]=0;
for(int i=2;i<=strlen(t);i++){
int k=nex[i-1];
while(k>=0&&t[k]!=t[i-1]) k=nex[k];
nex[i]=k+1;
}
}
// calculate the nextVal array for improved KMP algorithm
void CalcNextVal(char t[],int nex[],int nextVal[]){
/* write your code */
nextVal[0]=-1,nextVal[1]=0;
int i=0,j=-1;
while(i<strlen(t)){
if(j==-1||t[i]==t[j]){
i++,j++;
if(t[i]!=t[j]) nextVal[i]=j;
else nextVal[i]=nextVal[j];
}
else j=nextVal[j];
}
}
// using KMP algorithm to find the position of T in S
int findPos_kmp(char s[], char t[], int nex[]){
int count=0;
int m = strlen(s), n = strlen(t);
int i=0,j=0;
while(i<m && j<n){
count++;
if(j==-1||s[i]==t[j]){
i++,j++;
}
else{
j=nex[j];
}
}
cout<<"count in findPos_kmp is:"<<count<<endl;
if(j>=n) return i-n;
else return -1;
}
/*int main(){
// char S[]="ababcabcac",T[]="abcac",T2[]=" ababc";
// char S[]="aaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaabaaaaaaaab",T[]="aaaaaaaaab",T2[]=" aaaab";
// freopen("1.txt","r",stdin);
char S[max], T[max];
while(cin >> S >> T){
cout<<S<<endl;
cout<<T<<endl;
cout<<"the length of S and T is " << strlen(S) << " and " << strlen(T) << endl;
int pos = findPos(S,T);
cout<<"findPos:"<<pos<<endl;
cout<<endl;
int next[strlen(T)];
CalcNext(T,next);
cout<<"next array is:";
for(int i=0;i<strlen(T);i++){
cout<<next[i]<<" ";
}
cout<<endl;
cout<<endl;
pos=findPos_kmp(S,T,next);
cout<<"findPos_kmp:"<<pos<<endl;
cout<<endl;
int nextVal[strlen(T)+1];
CalcNextVal(T,next,nextVal);
cout<<"nextVal array is:";
for(int i=1;i<strlen(T);i++){
cout<<nextVal[i]<<" ";
}
cout<<endl;
cout<<endl;
pos=findPos_kmp(S,T,nextVal);
cout<<"findPos_improved_kmp:"<<pos<<endl;
cout<<endl;
}
return 0;
}*/