/*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. CSS3实现各种表情

    CSS3实现各种表情 效果图: 代码如下,复制即可使用: <!DOCTYPE html> <html> <head> <title></title ...

  2. tab切换webuploader失效的解决方法

    <script type="text/javascript"> $(document).ready(function () { $('#tt').tabs({ bord ...

  3. Windows下 ffmpeg + labelImg 提取视频帧 得到图片集 并 标注图片 来 构造数据集

    构造数据集的流程 视频文件  >>  ffmpeg处理  >>  图片集  >>  labelImg进行标注  >>  标注好的数据集 准备ffmpeg ...

  4. ZOJ Monthly, March 2018 题解

    [题目链接] A. ZOJ 4004 - Easy Number Game 首先肯定是选择值最小的 $2*m$ 进行操作,这些数在操作的时候每次取一个最大的和最小的相乘是最优的. #include & ...

  5. 015.Zabbix的日志监控配置

    一 日志监控概述 Zabbix可用于集中监控和分析日志,支持有日志轮询的日志监控分析.当日志中出现相关警告信息(如警告.报错等),可以发送通知给用户.日志监控功能,必须满足以下两个条件: Zabbix ...

  6. ApiPost自动化测试基础之:接口参数依赖的情景处理

    在<ApiPost环境变量之第1课>里,我们介绍了什么是ApiPost环境变量,并如何定义.使用它. 环境变量.接口参数依赖的处理是ApiPost自动化测试的基础.本文主要讲解接口参数依赖 ...

  7. BZOJ.1001.[BeiJing2006]狼抓兔子(最小割ISAP)

    题目链接 为什么这题网络流这么快,海拔那题就那么慢.. //119968kb 544ms //路不是有向的,所以要建四条边..既然如此就直接将反向边的流量设为w了.(or MLE...) #inclu ...

  8. C/C++的64为长整型数的表示

    在C/C++中,64为整型一直是一种没有确定规范的数据类型.现今主流的编译器中,对64为整型的支持也是标准不一,形态各异.一般来说,64位整型的定义方式有long long和__int64两种(VC还 ...

  9. BZOJ4065 : [Cerc2012]Graphic Madness

    因为两棵树中间只有k条边,所以这些边一定要用到. 对于每棵树分别考虑: 如果一个点往下连着两个点,那么这个点往上的那条边一定不能用到. 如果一个点往下连着一个点,那么这个点往上的那条边一定不能用到. ...

  10. 吴恩达-coursera-机器学习-week3

    六.逻辑回归(Logistic Regression) 6.1 分类问题 6.2 假说表示 6.3 判定边界 6.4 代价函数 6.5 简化的成本函数和梯度下降 6.6 高级优化 6.7 多类别分类: ...