String Matching(poj1580)
/*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)的更多相关文章
- 【HDOJ6629】string matching(exkmp)
题意:给定一个长为n的字符串,求其每个位置开始于其自身暴力匹配出相同或不同的结果的总比较次数 n<=1e6 思路:exkmp板子 #include<bits/stdc++.h> us ...
- C++ Primer 学习笔记_32_STL实践与分析(6) --再谈string类型(下)
STL实践与分析 --再谈string类型(下) 四.string类型的查找操作 string类型提供了6种查找函数,每种函数以不同形式的find命名.这些操作所有返回string::size_typ ...
- 通过Java字节码发现有趣的内幕之String篇(上)(转)
原文出处: jaffa 很多时候我们在编写Java代码时,判断和猜测代码问题时主要是通过运行结果来得到答案,本博文主要是想通过Java字节码的方式来进一步求证我们已知的东西.这里没有对Java字节码知 ...
- String.format(转)
转自:http://blog.csdn.net/lonely_fireworks/article/details/7962171 方便自己查阅. 常规类型的格式化 String类的format()方法 ...
- STL:string 大小(Size)和容量(Capacity)
strings存在三种“大小”: 1.size()和length() 返回string中现在的字符个数.上述两个函数等效. 成员函数empty()用来检验字符数是否为0,亦即字符串是否为空.你应该优先 ...
- 跟着刚哥梳理java知识点——深入理解String类(九)
一.String类 想要了解一个类,最好的办法就是看这个类的实现源代码,来看一下String类的源码: public final class String implements java.io.Ser ...
- Java基础——String类(二)
今天做了几道String常见操作.先来几个代码实例: 例一:此方法,仅把字符串前后出现的空格去掉了,中间部分不会. class TestTrim { public static void main(S ...
- String.format(2)
转载:https://blog.csdn.net/feng_870906/article/details/6870788 String.format是在JDK1.5中新增的静态方法,功能强.它主要功能 ...
- string 类(二)
处理string对象中的字符: 在cctype头文件中定义了一组标准库函数来处理string对象中的字符,比如检查一个string对象是否包含空白,或者把string对象中的字母改成小写,再或者查看某 ...
随机推荐
- (转载)How browsers work--Behind the scenes of modern web browsers (前端必读)
浏览器可以被认为是使用最广泛的软件,本文将介绍浏览器的工 作原理,我们将看到,从你在地址栏输入google.com到你看到google主页过程中都发生了什么. 将讨论的浏览器 今天,有五种主流浏览器— ...
- for-in循环(for-in Loops)
for-in循环应该用在非数组对象的遍历上,使用for-in进行循环也被称为“枚举”. 从技术上将,你可以使用for-in循环数组(因为JavaScript中数组也是对象),但这是不推荐的.因为如果数 ...
- K-Means和K Nearest Neighbor
来自酷壳: http://coolshell.cn/articles/7779.html http://coolshell.cn/articles/8052.html
- 兼容到ie10的js文件导出、下载到本地
话不多说,上代码: try { let reader = new FileReader(); let blob = new Blob([res.data], { type: 'application/ ...
- 008 jquery过滤选择器-----------(子元素过滤选择器)
1.介紹 2.程序 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...
- 在谈PHP中的 抽象类(abstract class)和 接口(interface)
一. 抽象类abstract class 1 .抽象类是指在 class 前加了 abstract 关键字且存在抽象方法(在类方法 function 关键字前加了 abstract 关键字)的类. 2 ...
- Windows 下 MySql 5.7.20安装及data和my.ini文件的配置(转)
Windows 下 MySql 5.7.20安装及data和my.ini文件的配置 本文通过图文并茂的形式给大家介绍了MySql 5.7.20安装及data和my.ini文件的配置方法. my ...
- Java中实现多线程的两种方式之间的区别
Java提供了线程类Thread来创建多线程的程序.其实,创建线程与创建普通的类的对象的操作是一样的,而线程就是Thread类或其子类的实例对象.每个Thread对象描述了一个单独的线程.要产生一个线 ...
- BZOJ4556 HEOI2016字符串
没错,又是这题,使用后缀自动机,反向建树,主席树维护right集合. By:大奕哥 #include<bits/stdc++.h> using namespace std; ; ]; ch ...
- IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!
Xtreme9.0 - Digit Fun! 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/di ...