/*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. Sublime Text 使用介绍/全套快捷键及插件推荐

    如果说Notepad++是一款不错Code神器,那么Sublime Text应当称得上是神器滴哥.Sublime Text最大的优点就是跨平台,Mac和Windows均可完美使用:其次是强大的插件支持 ...

  2. java代码分页

    分页类 这个适用情况: 适用于前端页面已提供分页按钮样式的情况 分页规则: 首页,尾页,上页,下页 这四个按钮必定出现,中间分页动态生成5个 如:首 上 2 3 4 5 6 下 尾 public cl ...

  3. 使用CSS3改变文本选中的默认颜色

    ::selection { background:#d3d3d3; color:#555; } ::-moz-selection { background:#d3d3d3; color:#555; } ...

  4. 一些计数小Trick

    一些计数小Trick 虽然说计数问题如果不是特别傻逼的话想做出来基本随缘. 但是掌握一些基本的计数方法还是十分有必要的. 想到了就更新. 1. 对于排列的DP问题,一般是不能够按照位置一个一个放的,一 ...

  5. NET WebAPi之断点续传下载(下)

    NET WebAPi之断点续传下载(下) 前言 上一篇我们穿插了C#的内容,本篇我们继续来讲讲webapi中断点续传的其他情况以及利用webclient来实现断点续传,至此关于webapi断点续传下载 ...

  6. 《阿里巴巴Java开发手册》扫描插件正式发布--插件安装和使用分析

    "不管做什么,只要坚持下去就会看到不一样!在路上,不卑不亢!" 阿里巴巴于10月14日上午9:00在杭州云栖大会<研发效能峰会>上,正式发布<阿里巴巴Java开发 ...

  7. GBDT算法

    GBDT通过多轮迭代,每轮迭代产生一个弱分类器,其中弱分类器通常选择为CART树,每个分类器在上一轮分类器的残差基础上进行训练. 对于GBDT算法,其中重要的知识点为: 1.GBDT是梯度下降法从参数 ...

  8. Win10 主题 美化 动漫

    韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha  313134555@qq.com High School D×D 塔城白音Win7主题+Win8主题+Win10主题 Win10 ...

  9. BZOJ.4407.于神之怒加强版(莫比乌斯反演)

    题目链接 Description 求\[\sum_{i=1}^n\sum_{j=1}^m\gcd(i,j)^K\ \mod\ 10^9+7\] Solution 前面部分依旧套路. \[\begin{ ...

  10. 如何利用Reveal神器查看各大APP UI搭建层级

    作者 乔同X2016.08.22 19:45 写了3195字,被42人关注,获得了73个喜欢 如何利用Reveal神器查看各大APP UI搭建层级 字数413 阅读110 评论0 喜欢5 title: ...