[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 ...
随机推荐
- SDN基础
http://www.h3c.com.cn/Solution/Smart_Network/SDN/ http://network.51cto.com/network/content2013/SDNke ...
- 【原生js】js动态添加dom,如何绑定事件
首先要明白浏览器在加载页面的时候是按顺序来加载的,这样以来就很清楚了,js动态添加dom以后,这些dom并没有绑定事件,这个时候最简单的一个办法就是:将绑定事件的方法封装到一个函数A中,在动态添加完d ...
- eclipse 一些快捷键
快捷键 alt + 上下方向键 向后缩进 shift + tab 整体向左移动 tab 就是向右移动 ctrl + Q 就是构建有参的构造方法 ctrl + E 是get set 方法,要把quick ...
- jquery css hover
<script type="text/javascript"> $(function () { $("#<%=btnSubmit.ClientID%&g ...
- Scala 控制结构
Scala内建的控制结构屈指可数,仅有if, while, for, match, try和函数调用而已. 几乎所有的Scala控制结构都会产生某个值(while和 do while虽然不能产生有意义 ...
- 技能学习经验与C语言学习调查
技能学习经验与C语言学习调查 前言 要说的话,这还是我第一次写博客.不论是为了作业也好,为了将来的学习工作也好,写博客都是必不可少的,也算是个自我提升的途径吧.不过第一次写博客,就用从来没听说过的ma ...
- 【Python】回文
首先判断一个字符串是否回文: def isPlalind(s): if len(s) == 1: return True else: return s[0] == s[-1] and isPlalin ...
- joda-time的一个DEMO
Date activeDate = person.getActiveTime(); if(activeDate==null){ modelMap.put("expireDate", ...
- 免费DDOS攻击测试工具大合集
FreeBuf微科普: DoS(Denial Of Service)攻击是指故意的攻击网络协议实现的缺陷或直接通过野蛮手段残忍地耗尽被攻击对象的资源,目的是让目标计算机或网络无法提供正常的服务或资源访 ...
- java学习开题