String Matching

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 847    Accepted Submission(s): 434
Problem Description
It's easy to tell if two words are identical - just check the letters. But how do you tell if two words are almost identical? And how close is "almost"?



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.
 
Sample Input
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.
 
Sample Output
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 【暴力】的更多相关文章

  1. Binary String Matching

    问题 B: Binary String Matching 时间限制: 3 Sec  内存限制: 128 MB提交: 4  解决: 2[提交][状态][讨论版] 题目描述 Given two strin ...

  2. NYOJ之Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述     Given two strings A and B, whose a ...

  3. ACM Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  4. 南阳OJ----Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  5. Binary String Matching(kmp+str)

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  6. Aho - Corasick string matching algorithm

    Aho - Corasick string matching algorithm 俗称:多模式匹配算法,它是对 Knuth - Morris - pratt algorithm (单模式匹配算法) 形 ...

  7. [POJ] String Matching

    String Matching Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4074   Accepted: 2077 D ...

  8. String Matching Content Length

    hihocoder #1059 :String Matching Content Length 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 We define the ...

  9. NYOJ 5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

随机推荐

  1. [SNOI2019]数论

    题目 考虑对于每一个\(a_i\)计算有多少个\(0<x\leq T-1\)满足\(x\equiv a_i(mod\ P)\)且\(x\ mod\ Q \in B\) 显然\(x=a_i+k\t ...

  2. SQL Server连接不上本地服务器

    昨天星期一,到公司,如常打开电脑后,上个厕所,吃个早餐,电脑才完全醒来.打开项目后台,发现登不上,用户名或密码错误,认真输入几遍,还是错误,打开本地数据库,sql server连接不上,提示错误: 我 ...

  3. [LOJ] 分块九题 6

    单点插入,单点查询. 优化了的链表. 链表老写错,干脆用vector,也不算慢. 注意链表退化的问题,及时(比如操作根号n次)就重新建块,实测速度可以提高一倍,这还是数据随机的情况,若涉及大量同一位置 ...

  4. uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 无效的列类型: 1111; nested exception is java.sql.SQLException: 无效的列类型: 1111

    WHERE 的条件取值时添加上jdbcType=NUMBER这样的配置 参考[1]:https://blog.csdn.net/fyhjuyol/article/details/45483167

  5. jquery 打星评分插件

    <link rel="stylesheet" href="/static/vendor/raty/jquery.raty.css"> <scr ...

  6. HTML中复选框的使用方法

    <select id="question"> {# 常见问题.ajax用editor.html('1231254')填充#} <option value=&quo ...

  7. openstack创建虚拟机之后使用ssh登陆的解决办法

    创建一个虚机之后:若果想要在horizon的控制台上登录操作,第一步.需要先使用ssh从controller上修改密码 从controller上登录: ssh ubuntu@虚机ip sudo su ...

  8. 活动预告丨易盾CTO朱浩齐将出席2018 AIIA大会,分享《人工智能在内容安全的应用实践》

    本文来自网易云社区 对于很多人来讲,仿佛昨天才燃起来的人工智能之火,转眼间烧遍了各个角落,如今我们的生活中,处处渗透着人工智能.10月16日,2018年 AIIA人工智能开发者大会在苏州举办,网易云易 ...

  9. [Go]通道(channel)的基本操作

    通道类型是Go语言自带的.唯一一个可以满足并发安全性的类型,在声明并初始化一个通道时,需要用到内建函数make,传给make函数的第一个参数应该代表通道的具体类型的类型字面量. 如类型字面量 chan ...

  10. 两行代码搞定UI主流框架

    XCNavTab XCNavTab适用于快速搭建NavigationController和TabBarController相结合的框架 https://github.com/xiaocaiabc/XC ...