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. POJ-1200-Crazy Search(字符串Hash)

    Crazy Search Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33142 Accepted: 9079 Descrip ...

  2. python实现快速排序、冒泡

    快速排序:首先任意选取一个数据(通常选用数组的第一个参数)作为关键数据,然后将比它小的数放在它前面,比它大的数放在后面,这个过程称之为快速排序 def quick_sort(l): if len(l) ...

  3. API对接中经常会出现的签名获取,这只是某一种,仅供给有需要的人参考

    要求: 1.对所有传入参数(含系统参数和接口参数)按照字段名的 ASCII 码从小到大排序(字典序)后,使用 URL 键值对的格式.(即 key1=value1&key2=value2…)拼接 ...

  4. 8. Truncate undo表空间

    8. Truncate undo表空间 要Truncate Undo 表空间,必须为MySQL实例配置至少两个undo表空间(两个undo表空间可确保一个undo表空间保持活动状态,另一个处于脱机状态 ...

  5. Django框架基础知识13-auth系统

    我们昨天登录admin时创建的用户信息是存放在哪里了呢? auth系统的数据表: 从表的名称我们就能看出, auth_user,auth_group,auth_permission分别存放了用户,用户 ...

  6. 牛客网 牛可乐发红包脱单ACM赛 B题 小a的旅行计划

    [题解] 题意其实就是把n个物品分成4个集合,其中三个集合不可以为空(只属于A.只属于B.AB的交),一个集合空或者非空都可以(不属于A也不属于B),问有多少种方案. 考虑容斥,4个集合都不为空的方案 ...

  7. 杭电 5326 Work (并查集求子结点为k的结点数)

    Description It’s an interesting experience to move from ICPC to work, end my college life and start ...

  8. SSRS ( .rdl文件)如何动态的设置导出Excel文件中的工作表标签名

    要实现以上效果,则在Tablix属性里设置 参考:https://dotblogs.com.tw/ricochen/archive/2012/06/14/72798.aspx

  9. 【转】亿级Web系统搭建——单机到分布式集群

    当一个Web系统从日访问量10万逐步增长到1000万,甚至超过1亿的过程中,Web系统承受的压力会越来越大,在这个过程中,我们会遇到很多的问题.为了解决这些性能压力带来问题,我们需要在Web系统架构层 ...

  10. python协程有多厉害?

    爬一个××网站上的东西,测算了一下协程的速度提升到底有多大,网站链接就不放了... import requests from bs4 import BeautifulSoup as sb import ...