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
  1. The input for your program will be a series of words, two per line, until the end-of-file flag of -1.
  2. Using the above technique, you are to calculate appx() for the pair of words on the line and print the result. For example:
  3. CAR CART
  4. TURKEY CHICKEN
  5. MONEY POVERTY
  6. ROUGH PESKY
  7. A A
  8. -1
  9. The words will all be uppercase.
 
Sample Output
  1. Print the value for appx() for each pair as a reduced fraction, like this:
  2. appx(CAR,CART) = 6/7
  3. appx(TURKEY,CHICKEN) = 4/13
  4. appx(MONEY,POVERTY) = 1/3
  5. appx(ROUGH,PESKY) = 0
  6. appx(A,A) = 1

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define maxn 1002
  4. char s1[maxn], s2[maxn];
  5. int len1, len2, ans, len;
  6.  
  7. void appx(){
  8. int num;
  9. int begin1 = 0, begin2 = len2 - 1;
  10. while(begin2 >= 0){
  11. num = 0;
  12. int i = begin1, j = begin2;
  13. while(i < len1 && j < len2){
  14. if(s1[i++] == s2[j++]) ++num;
  15. }
  16. if(num > ans) ans = num;
  17. --begin2;
  18. }
  19. begin2 = begin1 = 0;
  20. while(begin1 < len1){
  21. num = 0;
  22. int i = begin1, j = begin2;
  23. while(i < len1 && j < len2){
  24. if(s1[i++] == s2[j++]) ++num;
  25. }
  26. if(num > ans) ans = num;
  27. ++begin1;
  28. }
  29. }
  30. int gcd(int i, int j){
  31. return !j ? i : gcd(j, i % j);
  32. }
  33. void huajian(){
  34. int t = gcd(ans, len);
  35. ans /= t; len /= t;
  36. }
  37.  
  38. int main(){
  39. while(scanf("%s", s1), s1[0] != '-'){
  40. scanf("%s", s2);
  41. len1 = strlen(s1);
  42. len2 = strlen(s2);
  43. ans = 0;
  44. appx();
  45. len = len1 + len2;
  46. ans *= 2;
  47. printf("appx(%s,%s) = ", s1, s2);
  48. if(ans == 0 || ans == len){
  49. printf("%d\n", ans / len);
  50. continue;
  51. }
  52. huajian();
  53. printf("%d/%d\n", ans, len);
  54. }
  55. return 0;
  56. }

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. 光猫&路由器网络配置

    前期准备:电脑(工业电脑).网线.光猫.路由器 1.检查连接光猫后能否正常上网:把网线两头的水晶头,一头插在光猫上的千兆口,一头插在电脑(工业电脑)的网口上,看电脑能否正常上网: 可以正常上网:说明光 ...

  2. Java笔记:编写第一个Java程序

    2017.6.17 1.编写第一个Java程序 创建text文本,命名第一个Java程序.txt 在里面编写Java代码 public class Demo1{ public static void ...

  3. ios字体简单设定

    UILabel *lable = [[UILabel alloc] init]; label.font = [];

  4. spring注解开发-IOC

    1. @Configuration, @Bean @Configuration该注解就是用来告诉spring这是配置类 @Bean该注解是用来注册一个bean.类型是返回值的类型,ID默认是用方法名作 ...

  5. 【2018 CCPC网络赛】1001 - 优先队列&贪心

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6438 获得最大的利润,将元素依次入栈,期中只要碰到比队顶元素大的,就吧队顶元素卖出去,答案加上他们期中 ...

  6. Socket通信时服务端无响应,客户端超时设置

    背景:在写一个客户端的socket程序,服务端没有返回消息,客户端一直在等待. 目标:我需要设置一个时间,如果超过这个时间客户端自动断开连接.最好是在服务端实现,客户端对我来说不可控.

  7. Mysql+MHA高可用集群

    http://www.ttlsa.com/mysql/step-one-by-one-deploy-mysql-mha-cluster/

  8. centos7 firewalld日常使用

    若生产中使用有docker,建议不要使用firewalld,改用iptables,用firewalld坑很多,暂时还未找到解决办法,在此做下记录: 说明:若加参数permanent为永久添加即添加至z ...

  9. 如何使用GoEasy实现PHP与Websocket实时通信

    最近搞了搞websocket 做了个简答的聊天demo 1.      从GoEasy获取appkey appkey是验证用户的有效性的唯一标识. Ø  注册账号. GoEasy官网:https:// ...

  10. (二十二)python 3 sort()与sorted()

    Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_list = [3 ...