HDU1306 String Matching 【暴力】
String Matching
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.
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. For example: CAR CART
TURKEY CHICKEN
MONEY POVERTY
ROUGH PESKY
A A
-1 The words will all be uppercase.
Print the value for appx() for each pair as a reduced fraction, like this: appx(CAR,CART) = 6/7
appx(TURKEY,CHICKEN) = 4/13
appx(MONEY,POVERTY) = 1/3
appx(ROUGH,PESKY) = 0
appx(A,A) = 1
#include <stdio.h>
#include <string.h>
#define maxn 1002
char s1[maxn], s2[maxn];
int len1, len2, ans, len; void appx(){
int num;
int begin1 = 0, begin2 = len2 - 1;
while(begin2 >= 0){
num = 0;
int i = begin1, j = begin2;
while(i < len1 && j < len2){
if(s1[i++] == s2[j++]) ++num;
}
if(num > ans) ans = num;
--begin2;
}
begin2 = begin1 = 0;
while(begin1 < len1){
num = 0;
int i = begin1, j = begin2;
while(i < len1 && j < len2){
if(s1[i++] == s2[j++]) ++num;
}
if(num > ans) ans = num;
++begin1;
}
}
int gcd(int i, int j){
return !j ? i : gcd(j, i % j);
}
void huajian(){
int t = gcd(ans, len);
ans /= t; len /= t;
} int main(){
while(scanf("%s", s1), s1[0] != '-'){
scanf("%s", s2);
len1 = strlen(s1);
len2 = strlen(s2);
ans = 0;
appx();
len = len1 + len2;
ans *= 2;
printf("appx(%s,%s) = ", s1, s2);
if(ans == 0 || ans == len){
printf("%d\n", ans / len);
continue;
}
huajian();
printf("%d/%d\n", ans, len);
}
return 0;
}
HDU1306 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 (单模式匹配算法) 形 ...
- [POJ] String Matching
String Matching Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4074 Accepted: 2077 D ...
- 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 ...
随机推荐
- JavaEE-05 分页与文件上传
学习要点 新闻分页显示数据 新闻图片上传 JSP分页显示数据 分页 数据信息较多的的时候一般采用列表显示,方便展示信息: 数据量较大的时候一般采用列表加分页的方式显示,便于阅读. 分页方式:集合或者s ...
- assembly|reads to contig|contig to scaffold|coverage|depth| tandem repeats
(组装方面):SOAPdenovo ,因为采用de Bruijn graph algorithm算法和stepwise strategy ,所以排错能力高,所以我们获得高质量数据. de Bruijn ...
- Swift详解之NSPredicate
言:谓词在集合过滤以及CoreData中有着广泛的应用.本文以Playground上的Swift代码为例,讲解如何使用NSPredicate. 准备工作 先在Playground上建立一个数组,为后文 ...
- focus,focusin,blur,focusout区别
focus与focusin 1.共同点:当 <div> 元素或其任意子元素获得焦点时执行事件 2.区别:focus不支持冒泡,而focusin支持冒泡: blur与focusout 1.共 ...
- 厚溥教育1718部数据库连接作业答案,分装一个操作数据库而无需写SQL语句的函数
<?php header("Content-type:text/html;charset=utf8"); //PHP操作数据库的函数 function phpsql($dbc ...
- python中unicode, hex, bin之间的转换
python中unicode, hex, bin之间的转换 背景 在smb中有个feature change notify, 需要改动文件权限dacl,然后确认是否有收到notify.一直得不到这个d ...
- 《C/C++工程师综合练习卷》
前言 前天拿这个<C/C++工程师综合练习卷>练习了一下,现将错题以及精题分析总结. 错题分析与总结 2 . 下面的程序可以从1-.n中随机等概率的输出m个不重复的数.这里我们假设n远大于 ...
- jquery select 常用操作总结
由于在项目各种所需,经常碰到select不种操作的要求,今天特意总结了一下,分享: jQuery获取Select选择的Text和Value: 语法解释: 1. $("#select_id&q ...
- Fidder详解-抓取HTTPS清求(Web/App)抓包分析(靠谱篇)
为什么要学Fidder抓包? 学习接口,必须要学http协议,不要求您对协议的掌握有多深.只是希望你能够了解什么是协议.协议的报文.状态码等等!本文通过抓包工具Fidder带你进入接口的大门.我们通过 ...
- DBCA建库出错ORA-00600: internal error code, arguments
正常步骤安装完成Oralce,通过dbca建库,报错如下图所示: Oracle安装日志中报错如下: [Thread-40] [ 1999-12-15 12:23:54.055 CST ] [Basic ...