String Matching
Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 23   Accepted Submission(s) : 7
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.

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.
The words will all be uppercase.

Output
Print the value for appx() for each pair as a reduced fraction,Fractions reducing to zero or one should have no denominator.

Sample Input
CAR CART
TURKEY CHICKEN
MONEY POVERTY
ROUGH PESKY
A A
-1

Sample Output
appx(CAR,CART) = 6/7
appx(TURKEY,CHICKEN) = 4/13
appx(MONEY,POVERTY) = 1/3
appx(ROUGH,PESKY) = 0
appx(A,A) = 1

Source
PKU

#include <stdio.h>
#include <string.h>
#define MAX 1000

char str1[MAX];
char str2[MAX];

int common(int i,int j,int len1,int len2)
{
 int k=0;
 for(;i<len1;i++)
 {
  if(str1[i]==str2[j])
  k++;
        j++;
  if(j>=len2) break;
 }
 return k;
}
int gcd(int a,int b)
{
 int t=1;
 while(t)
 {
  t=b%a;
  b=a;
  a=t;
 }
 return b; 
}
int main()
{
 while(scanf("%s",str1)&&(strcmp(str1,"-1")!=0))
 {
  int i,j,k,t,len1,len2,num;
  scanf("%s",str2);
  len1=strlen(str1);
  len2=strlen(str2);
  num=t=k=0;
  for(i=0;i<len1;i++)
  {
   for(j=0;j<len2;j++)
   {
    if(str1[i]==str2[j])
    {
     t=common(i,j,len1,len2);
     if(t>num)
     {num=t;t=0;}
     break;
    }
   }
  }
  if(num==0)
  printf("appx(%s,%s) = 0\n",str1,str2);
  else if(num*2==len1+len2)
  printf("appx(%s,%s) = 1\n",str1,str2);
  else
  {
  k=gcd(num*2,len1+len2);
  printf("appx(%s,%s) = %d/%d\n",str1,str2,num*2/k,(len1+len2)/k);
  }
  //k=gcd(12,54);
  //printf("%d\n",k);
  //puts(str1);
  //puts(str2);
 }
 return 0;
}

【ACM】hdu_zs3_1005_String Matching_201308100920的更多相关文章

  1. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  2. 【ACM】HDU1008 Elevator 新手题前后不同的代码版本

    [前言] 很久没有纯粹的写写小代码,偶然想起要回炉再来,就去HDU随便选了个最基础的题,也不记得曾经AC过:最后吃惊的发现,思路完全不一样了,代码风格啥的也有不小的变化.希望是成长了一点点吧.后面定期 ...

  3. 【ACM】魔方十一题

    0. 前言打了两年的百度之星,都没进决赛.我最大的感受就是还是太弱,总结起来就是:人弱就要多做题,人傻就要多做题.题目还是按照分类做可能效果比较好,因此,就有了做几个系列的计划.这是系列中的第一个,解 ...

  4. 【ACM】那些年,我们挖(WA)过的最短路

    不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...

  5. 【ACM】不要62 (数位DP)

    题目:http://acm.acmcoder.com/showproblem.php?pid=2089 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新 ...

  6. 【Acm】八皇后问题

    八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题. 其解决办法和我以前发过的[算法之美—Fire Net:www.cnblogs.com/lcw/p/3159414.html]类似 题目:在8 ...

  7. 【ACM】hud1166 敌兵布阵(线段树)

    经验: cout 特别慢 如果要求速度 全部用 printf !!! 在学习线段树 内容来自:http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/24 ...

  8. 【acm】杀人游戏(hdu2211)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2211 杀人游戏 Time Limit: 3000/1000 MS (Java/Others)    M ...

  9. 【ACM】How many prime numbers

    http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2&sectionid=1&problemid=2 #inclu ...

随机推荐

  1. Fragment嵌套

    当我们从一个Activity启动了一个Fragment,然后在这个Fragment中又去实例化了一些子Fragment,在子Fragment中去有返回的启动了另外一个Activity,即通过start ...

  2. alsa 用户空间编程【转】

    本文转载自:http://blog.csdn.net/sjin_1314/article/details/12872581 /**alsa play test *ALSA用户空间编译,ALSA驱动的声 ...

  3. tensorflow在windows操作系统上的安装

    在电脑上安装PyCharm和Python3,然后把Python3的安装路径写进系统变量里,Python安装完之后, https://bootstrap.pypa.io/get-pip.py,把这页的代 ...

  4. SpringMVC之DispatcherServlet详解

    SpringMVC是一种基于请求启动的WEB框架,并且使用了前端控制器的设计模式,所有满足[WEB-INF/web.xml]文件中的[url-pattern]的匹配条件的请求,这些满足的请求都会交给这 ...

  5. thinkphp 日志记录

    日志记录\ThinkPHP\Lib\Think\Core\Log.class.php 1.可以在config.php中进行设置,默认为关闭状态. 'APP_DEBUG' => true 打开\T ...

  6. Python 41 多表查询 和 子查询

    1.查询             完整的查询语句             select [distinct] {* | 字段 | 聚合函数 | 表达式}from 表名                 ...

  7. WebService常用技术及术语

    一.Web Service是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实 ...

  8. WebApi中对请求参数和响应内容进行URL编码解码

    项目经测试,发现从IE提交的数据,汉字会变成乱码,实验了网上很多网友说的给ajax加上contentType:"application/x-www-form-urlencoded; char ...

  9. input表单(02)

    01.表单的代码实现 <!DOCTYPE html> <html> <head> <title>世纪佳缘,你在我也在</title> < ...

  10. 无桌面的linux 安装VMWare Tools

    1.在vmware虚拟机选项下,选择安装vmware-tools 2.将vmware安装目录下的linux.iso装载到系统中 2.1.选择需安装VMWareTools的虚拟机,右击--可移动设备-- ...