String Matching
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4074   Accepted: 2077

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<iostream>
#include<string>
using namespace std; int appx(string& word1,string&word2)
{
int count = 0;
int max = 0; int length1 = word1.length();
int length2 = word2.length(); for (int i = 0; i<length1; i++)
{
count = 0;
for (int j = 0; j<length2&&i + j<length1; j++)
{
if (word1[i + j] == word2[j])
count++;
}
if (max<count)
max=count;
} return max;
} int main()
{
string word1;
string word2; while (cin >> word1&&word1 != "-1")
{
cin >> word2;
int len1 = word1.length();
int len2 = word2.length(); int app1 = appx(word1,word2);
int app2 = appx(word2,word1); if (app1<app2)app1 = app2; cout << "appx(";
for (int i = 0; i<len1; i++)
cout << word1[i];
cout << ",";
for (int i = 0; i<len2; i++)
cout << word2[i];
cout << ") = "; if (app1 == 0)cout << 0 << endl;
else
{
len1 += len2;
app1 *= 2;
for (int i = 2; i <= ((len1<app1) ? len1 : app1); i++)
while (app1%i == 0 && len1%i == 0)
{
app1 /= i;
len1 /= i;
} if (app1%len1 != 0)
cout << app1 << '/' << len1 << endl;
else
cout << app1 / len1 << endl;
}
} return 0;
}

  

[POJ] String Matching的更多相关文章

  1. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

  2. NYOJ之Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述     Given two strings A and B, whose a ...

  3. ACM Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  4. 南阳OJ----Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  5. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  6. Aho - Corasick string matching algorithm

    Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...

  7. String Matching Content Length

    hihocoder #1059 :String Matching Content Length 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 We define the ...

  8. NYOJ 5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  9. (find) nyoj5-Binary String Matching

    5-Binary String Matching 内存限制:64MB 时间限制:3000ms 特判: No通过数:232 提交数:458 难度:3 题目描述: Given two strings A ...

随机推荐

  1. php学习笔记——语言切换

    现在的网站很多都可以实现多语言,于是记录一下多语言的实例. 方法一:通过将所有显示在页面的字段放在一个message文件里面来实现 思路如下图: test代码: main.php: <?php ...

  2. nodejs中express安装失败解决方法

    http://jingyan.baidu.com/article/922554468a3466851648f419.html

  3. Cg(C for Graphic)标准函数库之数学函数与几何函数

    和 C 的标准函数库类似, Cg 提供了一系列内建的标准函数.这些函数用于执行数学上的通用计算或通用算法(纹理映射等),例如,需要求取入射光线的反射光线方向向量可以使用标准函数库中的 reflect ...

  4. SQL总结之导入导出

    (5)还原数据库[倒库] [数据泵模式]先要在D盘创建个目录,如D:/dbback 然后去plsql中创建目录sql命令:create directory dbback as 'D:\dbback'; ...

  5. flume 以 kafka 为channel 的配置

    #此配置以kafka的一个topic为channel,相比其他channel类型 file和cache 兼并了快和安全的要求!# Define a kafka channel a1.channels. ...

  6. 提示找不到xml配置文件

    ClassPathXmlApplicationContext("applicationContext.xml")默认文件夹是resouerces,所以要把xml文件放在这个下面.

  7. js便利json 数组的方法

    这篇文章主要介绍了JQuery遍历json数组的3种方法,本文分别给出了使用each.for遍历json的方法,其中for又分成两种形式,需要的朋友可以参考下 $(function () { var ...

  8. 【Python】迭代器

    对迭代器和生成器的概念一直很混乱,总结一下: 迭代器: 1.所谓的迭代器,就是具有__next__()方法的对象: 2.__iter__()方法返回一个迭代器对象,这个对象必须具有__next__() ...

  9. jmeter对http协议中post请求接口测试

     现在有很多的工具用于工作上的使用,在jmeter的开源工具当中的,提供了一个可以对http协议的post的请求上接口测试,用于实现接口测试的自动化测试,当然也可以使用自己写的工具. 进行打开jmet ...

  10. cetos6 安装samba共享文件夹

    yum方式安装 yum install samba 修改配置文件 vim /etc/samba/smb.conf [global] comment = global workgroup = QFpay ...