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. CFRound#379(div2)

    题目链接:http://codeforces.com/contest/734 A:SB题. #include<cstdio> #include<cstring> #includ ...

  2. 效果网址http://sc.chinaz.com/tag_jiaoben/tupianlunbo.html

    http://sc.chinaz.com/tag_jiaoben/tupianlunbo.html

  3. Linux 操作命令列表记录

    Linux 操作命令列表记录 SSH登录 登录 ## 范式 ssh [username]@[host] ## 例 ssh -p 1222 root@10.0.0.1 使用非默认端口(ssh默认端口22 ...

  4. 远程连接mysql数据库,1130问题

    远程或使用非127.0.0.1和localhost地址连接时,出现代号为1130问题, ERROR 1130: Host 192.168.2.159 is not allowed to connect ...

  5. 第一百二十三节,JavaScript错误处理与调试

    JavaScript错误处理与调试 学习要点: 1.浏览器错误报告 2.错误处理 3.错误事件 4.错误处理策略 5.调试技术 6.调试工具 JavaScript在错误处理调试上一直是它的软肋,如果脚 ...

  6. sql注入示例

    实验指导说明 实验环境 • 实验环境 o 操作机:Windows XP o 目标机:Windows 2003 o 目标网址:www.test.ichunqiu • 实验工具: Tools Path S ...

  7. Java的“影子克隆”和“深度克隆”

    今天来学习学习java对象的克隆,在写代码的时候,有时候我们会这样写:对象1=对象2,也就是把对象2赋值给对象1了,但是这样做有个问题,就是如果我们修改了对象2的属性值,对象1的相同属性值也被修改了, ...

  8. Swift中GCD与NSOperation相关

    GCD Swift 3必看:从使用场景了解GCD新API 常用写法: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_ ...

  9. PEiD 0.95 全插件中文绿色版

    软件名称: PEiD 0.95 软件语言: 简体中文 授权方式: 免费软件 运行环境: Win8 / Win7 / Vista / WinXP 软件大小: 4.4MB 图片预览: 软件简介: PEiD ...

  10. spring使用jdbcTemplate和jdbcdaosupport和namedparameter

    jdbcTemplate: 首先连接数据库 <!-- 导入外部文件 --> <context:property-placeholder location="classpat ...