/*String Matching

Description





It's easy to tell if two words are identical - just check the letters. But how do you tell if two words are almost identical?

And how close is "almost"? 





There are lots of techniques for approximate word matching. One is to determine the best substring match, which is the number of common letters when the words are compared letter-byletter. 





The key to this approach is that the words can overlap in any way. For example, consider the words CAPILLARY and MARSUPIAL. One way to compare them is to overlay them: 





CAPILLARY 

MARSUPIAL 





There is only one common letter (A). Better is the following overlay: 

CAPILLARY





     MARSUPIAL





with two common letters (A and R), but the best is: 

   CAPILLARY





MARSUPIAL





Which has three common letters (P, I and L). 





The approximation measure appx(word1, word2) for two words is given by: 

common letters * 2 

----------------------------- 

length(word1) + length(word2)





Thus, for this example, appx(CAPILLARY, MARSUPIAL) = 6 / (9 + 9) = 1/3. Obviously, for any word W appx(W, W) = 1, which is a nice property, while words with no common letters have an appx value of 0.

Input





The input for your program will be a series of words, two per line, until the end-of-file flag of -1. 

Using the above technique, you are to calculate appx() for the pair of words on the line and print the result. 

The words will all be uppercase.

Output





Print the value for appx() for each pair as a reduced fraction,Fractions reducing to zero or one should have no denominator.

Sample Input





CAR CART

TURKEY CHICKEN

MONEY POVERTY

ROUGH PESKY

A A

-1

Sample Output





appx(CAR,CART) = 6/7

appx(TURKEY,CHICKEN) = 4/13

appx(MONEY,POVERTY) = 1/3

appx(ROUGH,PESKY) = 0

appx(A,A) = 1

*/

#include<stdio.h>

#include<string.h>

int gcd(int m,int n)//求最大公约数; 

{

if(n==0)

return m;

else

return gcd(n,m%n);

}

int main()

{

char a[100],b[100];

while(scanf("%s",a)!=EOF)

{

if(strcmp(a,"-1")==0)

break;

else

scanf("%s",b);

int i,j,k,l,max=0,t;

int len1,len2,len;

len1=strlen(a);

len2=strlen(b);

for(i=0;i<len1;i++)/*相当于a[len1]不动,从,i=j=0開始,b[i++]与a[j++]比較。同样的话t++,

之后b[0]与a[i]比較,至到a[len-1]与b[i]比較记下t,并与之前的t比較,得出更大的t。后面继续从b[1]继续比較,直到最好能比較结束*/ 

{

k=0;

for(l=i,j=0;l<len1;l++,j++)

{

if(a[l]==b[j])

k++;

}

max=max>k?

max:k;

}

for(i=0;i<len2;i++)//相当于b[len2]不动,与上面类似,比較easy举一反三。

{

k=0;

for(l=i,j=0;l<len2;l++,j++)

{

if(b[l]==a[j])

k++;

}

max=max>k?

max:k;

}

len=len1+len2,max*=2;

   t=gcd(max,len);

if(len==max)

{

   printf("appx(%s,%s) = 1\n",a,b);

   continue;//这个continue不能省略; 

   }

if(max==0)

{

   printf("appx(%s,%s) = 0\n",a,b);

       continue;

   }

else

printf("appx(%s,%s) = %d/%d\n",a,b,max/t,len/t);

}

return 0;

}

String Matching(poj1580)的更多相关文章

  1. 【HDOJ6629】string matching(exkmp)

    题意:给定一个长为n的字符串,求其每个位置开始于其自身暴力匹配出相同或不同的结果的总比较次数 n<=1e6 思路:exkmp板子 #include<bits/stdc++.h> us ...

  2. C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)

    STL实践与分析 --再谈string类型(下) 四.string类型的查找操作 string类型提供了6种查找函数,每种函数以不同形式的find命名.这些操作所有返回string::size_typ ...

  3. 通过Java字节码发现有趣的内幕之String篇(上)(转)

    原文出处: jaffa 很多时候我们在编写Java代码时,判断和猜测代码问题时主要是通过运行结果来得到答案,本博文主要是想通过Java字节码的方式来进一步求证我们已知的东西.这里没有对Java字节码知 ...

  4. String.format(转)

    转自:http://blog.csdn.net/lonely_fireworks/article/details/7962171 方便自己查阅. 常规类型的格式化 String类的format()方法 ...

  5. STL:string 大小(Size)和容量(Capacity)

    strings存在三种“大小”: 1.size()和length() 返回string中现在的字符个数.上述两个函数等效. 成员函数empty()用来检验字符数是否为0,亦即字符串是否为空.你应该优先 ...

  6. 跟着刚哥梳理java知识点——深入理解String类(九)

    一.String类 想要了解一个类,最好的办法就是看这个类的实现源代码,来看一下String类的源码: public final class String implements java.io.Ser ...

  7. Java基础——String类(二)

    今天做了几道String常见操作.先来几个代码实例: 例一:此方法,仅把字符串前后出现的空格去掉了,中间部分不会. class TestTrim { public static void main(S ...

  8. String.format(2)

    转载:https://blog.csdn.net/feng_870906/article/details/6870788 String.format是在JDK1.5中新增的静态方法,功能强.它主要功能 ...

  9. string 类(二)

    处理string对象中的字符: 在cctype头文件中定义了一组标准库函数来处理string对象中的字符,比如检查一个string对象是否包含空白,或者把string对象中的字母改成小写,再或者查看某 ...

随机推荐

  1. Java中关于HashMap源码的研究

    1.基础知识 1.数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1):数组的特点是:寻址容易,插入和删除困难. 2.链表 链表存储区间离散,占用内存比 ...

  2. SqlServer查看表、存储过程、耗时查询、当前进程、开销较大的语句

    --查看数据库中表的语句 SELECT s2.dbid , DB_NAME(s2.dbid) AS [数据库名] , --s1.sql_handle , ( SELECT TOP 1 SUBSTRIN ...

  3. 使用before_request来做权限和用户检查

    因为使用restful方式,因此每次用户访问都会上传带入auth_key,如jwt等,因此可在@app.before_request中做权限的检查. @app.app.before_request d ...

  4. SpringMVC JSON数据交互

    本节内容: @RequestBody @ResponseBody 请求json,响应json实现 前端可以有很多语言来写,但是基本上后台都是java开发的,除了c++(开发周期长),PHP和#Net( ...

  5. for-in循环(for-in Loops)

    for-in循环应该用在非数组对象的遍历上,使用for-in进行循环也被称为“枚举”. 从技术上将,你可以使用for-in循环数组(因为JavaScript中数组也是对象),但这是不推荐的.因为如果数 ...

  6. vs2010 打包安装

    https://jingyan.baidu.com/article/b7001fe184e4e50e7382dd4e.html 1 建立安装项目2 要安装的文件都添加到应用程序文件夹3 在应用程序文件 ...

  7. kebab HDU2883

    题意:现在有n个人要烤肉,有m个烤肉架,然后给出每个人的烤肉开始时间si,结束时间ei,以及要烤肉的串数num,还有拷一串的时间ti,然后问你能不能满足所有人的要求. 为3572的进阶题 每个人为一个 ...

  8. 040 DataFrame中的write与read编程

    一:SparkSQL支持的外部数据源 1.支持情况 2.External LIbraries 不是内嵌的,看起来不支持. 但是现在已经有很多开源插件,可以进行支持. 3.参考材料 · 支持的格式:ht ...

  9. vue.js阻止事件冒泡和默认事件

    首先我们来看原生JS取消事件冒泡方法: e.stopPropagation(); //非IE浏览器window.event.cancelBubble = true; //IE浏览器 原生JS阻止默认事 ...

  10. (转载)ACM训练计划,先过一遍基础再按此拼搏吧!!!!

    ACM大量习题题库 ACM大量习题题库 现在网上有许多题库,大多是可以在线评测,所以叫做Online Judge.除了USACO是为IOI准备外,其余几乎全部是大学的ACM竞赛题库. USACO ht ...