自动化系PB11210038张凯数据结构——文章相似度一问题叙述给出两篇文章,从文件中读入这两篇文章,求出其相似度,相似度定义为两篇文章的所有相同处(连续超过n个字认为相同)除以两篇文章中短篇字数。二算法描述1将文件中读入的文字存进字符串,对字符串进行操作2求这两个字符串的公共子串3如果公共子串中超过m个字,就认为该字符串抄袭,并求其长度4将所有抄袭字符串长度累计,除以短篇长度,得到相似度三程序实现#include<>#include<>#definesize100typedefstruct{ chardata[size]; intlength;}Sqstring;intStrlength(Sqstrings[size]){ inti=0; while(s->data[i]!='\0') i++; return(i+1);}monchar(intnum[][size],intlength1,intlength2,charpublics[],Sqstringstr1[]){intcount1=0,count2=0,i,j,m,n,flag,k,all=0;for(i=0;i<length1;i++)//遍历矩阵 for(j=0;j<length2;j++) { m=i; n=j; while(num[m][n]==1) { count1++;//计算相同的字符数 num[m][n]=0;//清零 m++;//对角线后移 n++; } /*if(count1>count2)//寻找最长子串{ count2=count1; flag=i; for(k=0;k<count2&&flag<m;k++,flag++) publics[k]=str1->data[flag];//将相似字符串赋予publics[k] }*/if(count1>5)all=all+count1; count1=0; } returnall;}intmain(){Sqstringstr1[size],str2[size];ints[size][size];charsub[size]={0};//公共字串inti,j,all,m,n;floatw;//相似度charch;FILE*fp1,*fp2;if((fp1=fopen("","r+"))==NULL){ printf("can'topentext1\n"); // exit(0); } if((fp2=fopen("","r+"))==NULL) { printf("can'topentext2\n"); //exit(0); } i=0; j=0; while((ch=fgetc(fp1))!=EOF)str1->data[i++]=ch; str1->data[i]='\0';while((ch=fgetc(fp2))!=EOF)str2->data[j++]=ch; str2->data[j]='\0'; //for(i=0;i<Strlength(str2);i++) //ch=s2[i]; printf("..\n");m=Strlength(str1); n=Strlength(str2);printf("str1=%d\n",m);printf("st
相似度数据结构 来自淘豆网m.daumloan.com转载请标明出处.