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 ...
随机推荐
- 定位 absolute和relative比较
absolute:脱离原来位置定位.是相对于最近的有定位的父级进行定位;如果没有有定位的父级元素,就相对文档进行定位 relative:保留原来位置进行定位,相对于自己原来的位置进行定位 下面举两个例 ...
- 模拟--P1540 机器翻译
题目连接 题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词 ...
- Openjudge-2815-城堡问题
对于这道题目来说的话,我们的思路是这样的,我们首先把数据读进来,然后把color数组清零. 我们的思路是这样的的,给每一个房间设置一个对应的color数组,然后color数组里面填满不同的数字,每一种 ...
- 内存区--Java
一.概述 对于 Java 程序员来说,在虚拟机自动内存管理机制下,不再需要像C/C++程序开发程序员这样为内一个 new 操作去写对应的 delete/free 操作,不容易出现内存泄漏和内存溢出问题 ...
- PHP将数据库的数据转换成json格式
header('content-type:application/json;charset=utf8'); $results = array(); while ($row = mysql_f ...
- 7 SQL 集合运算
7 集合运算 7-1 表的加减法 本章将会和大家一起学习“集合运算”操作.在数学领域,“集合”表示“(各种各样的)事物的总和”:在数据库领域,表示“记录的集合”.具体来说,表.视图和查询的执行结果都是 ...
- python之图形界面GUI开发 Tkinter 2014-4-7
1.导入Tkinter 可以使用以下三种方法(1)from Tkinter import *#导入Tkinter(2)import TkinterTkinter.methodA使用 Tkinter.m ...
- xtu summer individual 3 C.Infinite Maze
B. Infinite Maze time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【java基础 3】树形结构数据呈现的递归算法实现
一.基本概况 在我的项目中,常常会用到树形结构的数据,最为明显的就是左边菜单栏,类似于window folder一样的东西. 而我之前一直是借助前端封装好的ZTree等工具实现展示,而后台则通常使用递 ...
- B/S 开发和 C/S开发的区别
导读:每天都从应用中心下载很多软件安装尝试,在自己的电脑上也装了很多软件,但是,就出现了一个问题,好比QQ,为什么有了APP,还要有网站应用呢?由此,结合到自己的学习,就衍生出一个问题:C/S 开发就 ...