/*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. PHP操作Redis常用

    一.Redis连接与认证 //连接参数:ip.端口.连接超时时间,连接成功返回true,否则返回false $ret = $redis->connect('127.0.0.1', 6379, 3 ...

  2. 移动端console.log()调试

    在微信或app进行开发的时候,没法直接查看console.log的输出内容,调试起来简直太痛苦了. 1.笨笨的方法 fiddler抓请求:追加dom节点,显示调试信息. var div =docume ...

  3. python 单变量线性回归

      单变量线性回归(Linear Regression with One Variable)¶ In [54]: #初始化工作 import random import numpy as np imp ...

  4. VS C++ 并发编程

    1.VS2012及以上版本,支持C++11 thread类的并发编程. 相关材料可以参考博客:http://www.cnblogs.com/rangozhang/p/4468754.html 2.但对 ...

  5. 【noip模拟赛1】古韵之乞巧 (dp)

    描述 闺女求天女,更阑意未阑. 玉庭开粉席,罗袖捧金盘. 向月穿针易,临风整线难. 不知谁得巧,明旦试相看. ——祖咏<七夕> 女子乞巧,是七夕的重头戏.古时,女子擅长女红被视为一种重要的 ...

  6. myBatsi调用存储过程

    1.结构 2.准备数据 建表和插入数据 CREATE TABLE p_user( id INT PRIMARY KEY AUTO_INCREMENT, name ), sex ) ); INSERT ...

  7. 常见Java库漏洞汇总

    1.ActiveMQ 反序列化漏洞(CVE-2015-5254) ref:https://www.nanoxika.com/?p=408 Apache ActiveMQ是美国阿帕奇(Apache)软件 ...

  8. Codeforces Round 542 (Div. 2)

    layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  9. Java 持久化之 -- IO 全面整理(看了绝不后悔)

    目录: 一.java io 概述 什么是IO? IO包括输入流和输出流,输入流指的是将数据以字符或者字节形式读取到内存 分为字符输入流和字符输入流 输入流指的是从内存读取到外界 ,分为字符输入流和字节 ...

  10. 配置dcom时,在此计算机运行应用程序不可选

    Finally.... After installing windows 7 - 32 bit and seeing that DcomCnfg worked led me to believe th ...