📄 test_compare.cpp
字号:
// Test_Compare.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
#include<fstream>
#include<iomanip>
#include <string>
#include <stdlib.h>
#include <cstdlib>
using std::cout ;
using std::cin;
using std::vector ;
using std::string ;
using std::endl;
using std::iostream ;
using std::ios;
bool GetIn(const char *filename,vector<string> &V1)
{
std::ifstream InFile(filename);
if (!InFile)
return false;
while (!InFile.eof())
{
string s="";
if (InFile.eof()) break;
char c=InFile.get();
while(!InFile.eof() && c!='\n')
{
s+=c;
c=InFile.get ();
}
V1.push_back(s);
}
return true;
}
bool Init( const char *Sourcefilename,vector<string> &Source,const char * Destfilename,vector<string> &Dest)
{
GetIn(Sourcefilename,Source);
GetIn(Destfilename,Dest);
return true;
}
bool Get_MaxMatch(vector<string> &Source,vector<string> &Dest,std::ostream &out)
{
int m=Source.size ();
int n=Dest.size ();
int **P=new int*[m];
for(int i=0;i<m;i++)
P[i]=new int [n];
for (int i=0;i<m;i++)
{
for (int j=0;j<n;j++)
{
if (Source[i]==Dest[j])
P[i][j]=1;
else
P[i][j]=0;
}
}
int **Q=new int*[m+1];
for(int i=0;i<m+1;i++)
{
Q[i]=new int [n+1];
Q[i][n]=0;
}
for (int i=0;i<n+1;i++)
{
Q[m][i]=0;
}
for (int i=m-1;i>=0;i--)
{
for (int j=n-1;j>=0;j--)
{
int max;
int maxi;
int maxj;
if (Q[i][j+1]>= Q[i+1][j])
{
max=Q[i][j+1];
}
else
{
max=Q[i+1][j];
}
if (Q[i+1][j+1]+P[i][j]>max)
{
max=Q[i+1][j+1]+1;
}
Q[i][j]=max;
}
}
int max_Q=-1;
for (int i=0;i<m;i++)
if (P[i][0]==1 && (max_Q==-1 || Q[i][0]>max_Q))
{
max_Q=Q[i][0];
}
for (int i=0;i<n;i++)
if (P[0][i]==1 && Q[0][i]>max_Q)
{
max_Q=Q[0][i];
}
int s=max_Q;
vector<int> Path;
int h;
for (int i=0;i<m;i++)
{
if (P[i][0]==1 && Q[i][0]==max_Q)
{
int x=i;
int y=0;
Path.push_back(i);
Path.push_back(0);
s=s-1;
while(s>0)
{
bool signal=true;
int u;
int v;
int x1;
int y1;
for(u=x;u<m && signal ;u++)
for (v=y;v<n && signal;v++)
{
if (P[u][v]==1 && Q[u][v]==s)
{
Path.push_back(u);
Path.push_back (v);
x1=u;
y1=v;
signal=false;
}
}
x=x1;
y=y1;
s=s-1;
}
cout <<endl;
h=i;
break;
}
}
int ti=0;
int tj=0;
int si;
int sj;
int sum=1;
for (int k=0;k<Path.size() ;k=k+2)
{
si=Path[k];
sj=Path[k+1];
for (int j=ti;j<si;j++)
{
out<<"在第" << j+sum <<"行删除"<< Source[j]<<endl;
sum--;
}
for (int j=tj;j<sj;j++)
{
out<<"在第" << si+sum <<"行插入"<< Dest[j]<<endl;
sum++;
}
ti=si;
tj=sj;
ti++;
tj++;
}
si=m;
sj=n;
for (int j=ti;j<si;j++)
{
out<<"在第" << j+sum <<"行删除"<< Source[j]<<endl;
sum--;
}
for (int j=tj;j<sj;j++)
{
out<<"在第" << si+sum <<"行插入"<< Dest[j]<<endl;
sum++;
}
return true;
}
int main(int argc,char *argv[])
{
std::ifstream inFile1;
std::ifstream inFile2;
std::ofstream outFile;
if (argc !=4)
cout<<"格式 ma sourcefile destfile resultfile";
else
{
inFile1.open (argv[1]);
if(inFile1==NULL )
{
cout<<"源文件打不开";
}
else
{
inFile2.open(argv[2]);
if(inFile2==NULL )
{
cout<<"目标文件打不开";
}
else
{
outFile.open (argv[3]);
if(outFile==NULL)
cout<<"结果文件打不开";
else
{
vector<string> Source;
vector<string> Dest;
Init(argv[1],Source,argv[2],Dest);
Get_MaxMatch(Source,Dest,outFile );
}
}
}
}
const char *filename="F:\\1.txt";
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -