[POJ] String Matching
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 4074 | Accepted: 2077 |
Description
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
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
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的更多相关文章
- Binary String Matching
问题 B: Binary String Matching 时间限制: 3 Sec 内存限制: 128 MB提交: 4 解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...
- NYOJ之Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose a ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- 南阳OJ----Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Binary String Matching(kmp+str)
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Aho - Corasick string matching algorithm
Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...
- String Matching Content Length
hihocoder #1059 :String Matching Content Length 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 We define the ...
- NYOJ 5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- (find) nyoj5-Binary String Matching
5-Binary String Matching 内存限制:64MB 时间限制:3000ms 特判: No通过数:232 提交数:458 难度:3 题目描述: Given two strings A ...
随机推荐
- Java代理模式汇总
简介 代理模式即Proxy Pattern,23种java常用设计模式之一.其定义为:对其他对象提供一种代理以控制对这个对象的访问. UML类图 静态代理 目标接口 public interface ...
- XTU 1249 Rolling Variance
$2016$长城信息杯中国大学生程序设计竞赛中南邀请赛$G$题 前缀和. 把公式化开来,会发现只要求一段区间的和以及一段区间的平方和就可以了. #pragma comment(linker, &quo ...
- 子查询 此处该用AND 而不是 WHERE
条件:有一张账户表,一张订单表. 需求:求出所有role = 2 即客服人员,所有操作成功的订单数量.结果:能查出所有的客服人员名称,以及操作的订单数量(关键点在于,没有操作过订单,则数量显示为0) ...
- 《Intel汇编第5版》 条件汇编伪指令
一.条件汇编伪指令和宏使用可以使汇编程序更加灵活 二.通过伪指令来检查函数的参数是否为空,如果为空则输出警告信息 INCLUDE Irvine32.inc includelib Irvine32.li ...
- AngularJS 初学笔记(理论基础)
AngularJS 是一个 JavaScript 框架.它是一个以 JavaScript 编写的库. AngularJS 通过 ng-directives 扩展了 HTML. ng-app 指令定义一 ...
- checkbox:全选与反全选
$(document).ready(function () { //全选checkbox $("#selectAll").click(function () { var check ...
- 【转载】javadoc学习笔记和可能的注意细节
转载自:http://www.cnblogs.com/xt0810/p/3630996.html [前面的话] 这次开发项目使用jenkins做持续集成,PMD检查代码,Junit做单元测试,还会自动 ...
- 一个小时快速搭建微信小程序
「小程序」这个划时代的产品发布快一周了,互联网技术人都在摩拳擦掌,跃跃欲试.可是小程序目前还在内测,首批只发放了 200 个内测资格(泪流满面).本以为没有 AppID 这个月就与小程序无缘了,庆幸的 ...
- js格式转换
//1.保留整数 function showInteger(value,row,index){ if(value!=null && value!="" && ...
- Xcode-之CocoaPads
一.说明 CocoaPods是OS X和iOS下的一个第三类库管理工具,通过CocoaPods工具我们可以为项目添加被称为“Pods”的依赖库(这些类库必须是CocoaPods本身所支持的),并且可以 ...