[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 ...
随机推荐
- python3中字典的copy
字典是可变的: first和second同时指向一个字典.first修改也会影响second.在程序中一定注意对字典参数的修改会对原始的字典进行修改.这也体现了字典是可变的. 字典的copy方法是浅拷 ...
- Front-End(五)——工具使用
mac端推荐使用sublime+emmet. 环境搭建 sublime 官网下载sublime text 02或者03,03现在(2016.07)还是测试版,我使用的是text02. emmet su ...
- python代码随笔
此篇随笔只是作为自己偶然想起的遇到过的代码片段..记录下! 1.巧用lambda,reduce实现多层嵌套的装饰器: 示例如下: #示例 函数chain([a,b,c,d) (input), 最终实现 ...
- NGINX----源码阅读---have配置脚本
/auto/have have配置脚本负责在$NGX_OBJS/ngx_auto_config.h定义宏 # Copyright (C) Igor Sysoev # Copyright (C) Ngi ...
- redis介绍。
1. Redis是什么 这个问题的结果影响了我们怎么用Redis.如果你认为Redis是一个key value store, 那可能会用它来代替MySQL;如果认为它是一个可以持久化的cache, 可 ...
- haproxy(1)
参考文档: http://cbonte.github.io/haproxy-dconv/1.5/configuration.html 一.Haproxy 软件负载均衡一般通过两种方式来实现:基于操作系 ...
- Java资源大全中文版
awesome-java-cn 是 Java 资源大全的中文版,包括开发库.开发工具.网站.博客等,将由伯乐在线持续更新. https://github.com/jobbole/awesome-jav ...
- hdu 2087 剪花布条 kmp模板题
也是kuangbin专题的 专题名字太长 不复制了…… 刚好数据结构也学了kmp 找一道题敲敲模板…… 暴力的字符串匹配是O(n*m)的时间复杂度 而kmp通过一个O(m)的预处理将字符串匹配的时间复 ...
- Android抓包方法
0. Fiddler代理 1.tcpdump命令+wireshark工具 adb shell #登入手机 su #切换Root用户 /data/local/tcpdump -p ...
- 【Python之路】第八篇--Python基础之网络编程
Socket socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. sock ...