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 ...
随机推荐
- ES6 第一章 let和const命令 具体参照http://es6.ruanyifeng.com
1.let类似于var用用来定义变量 1)let没有预解析,不存在变量提升 // var 的情况 console.log(foo); // 输出undefined var foo = 2; // le ...
- docker运行时设置redis密码并替换redis默认的dump.rdb
docker run -itd --name test -p 6379:6379 -v /tmp/dump.rdb:/data/dump.rdb redis:4.0.8 --requirepass ' ...
- VS C++项目报错warning C4199: ……use /Zc:twoPhase-
具体的报错如下: c1xx : warning C4199: two-phase name lookup is not supported for C++/CLI, C++/CX, or OpenMP ...
- 树莓派搭建Seafile个人网盘
步骤一.安装Seafile依赖包 yum install python-setuptools python-ldap python-memcached MySQL-python mariadb mar ...
- 201621123079《java程序设计》第六周作业总结
作业06-接口.内部类 1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行一个总结. 2. 书面作业 1. c ...
- l5-repository基本使用--结合使用artisan
一.从头开始创建 1.执行以下artisan: php artisan make:entity Student 如果某个文件已经存在,则不会创建新的文件去覆盖原有的文件,案例如下: 2.修改model ...
- 上海苹果维修点分享苹果电脑MACBOOK故障维修常见案例
苹果的电子设备无论是外观和性能都是无与伦比的美丽,很多开发者都开始选用苹果电脑macbook.近年来苹果售后维修点来维修苹果电脑的用户也越来越多,我们上海苹果维修点就整理分享了一些苹果电脑MACBOO ...
- CUDA & cuDNN环境配置
环境 python3.5 tensorflow 1.3 VUDA 8.0 cuDNN V6.0 1.确保GPU驱动已经安装 lspci | grep -i nvidia 通过此命令可以查看GPU信息 ...
- Python中input()和raw_input()函数的区别
问题:在Python2.7中使用 input() 函数会出现 “NameError: Name ”***“ is not defined 的错误 解决: 使用raw_input() 函数,在Pytho ...
- URAL 2040 Palindromes and Super Abilities 2
Palindromes and Super Abilities 2Time Limit: 500MS Memory Limit: 102400KB 64bit IO Format: %I64d &am ...