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. n阶导函数存在与n阶可导的区别

    1.f(x)n阶导函数存在 <=======>  f(n)(x)存在  指的是在某个区间内有定义 2.f(x)n阶可导根据题意可以有两种不同的解释: ①.题目中说的是在某点即在x=x0处n ...

  2. 第11课 Git GUI程序的基本功能

    11-1 Git GUI程序的基本操作

  3. 0423-mysql插入语句大全

    /*注意: 1.字段和值要一一对应 2.值的数据类型是字段的数据类型 3.当输入的字段是表中全部字段时,字段可以省略不写: insert into login values ('zhangsan',‘ ...

  4. 可持久化Treap(fhq Treap,非旋转式Treap)学习(未完待续)

    简介:     Treap,一种表现优异的BST 优势:     其较于AVL.红黑树实现简单,浅显易懂     较于Splay常数小,通常用于树套BST表现远远优于Splay     或许有人想说S ...

  5. bzoj3687简单题(dp+bitset优化)

    3687: 简单题 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 861  Solved: 399[Submit][Status][Discuss] ...

  6. Websocket实现前后台通信,demo小测试

    新需求大概如下:用户登录系统,登录成功之后建立websocket连接,实现通信 总体思路:前端不是我负责,只是简单的做个功能,先实现登录,把用户标识存入HttpSeesion,再建立websocket ...

  7. UITextField 点击事件 --- 不会触发键盘弹出,触发其他事件的实现。

    今天在做项目的过程中,其中有三个控件: UITextField, UITextView , UILabel, 后来发现个问题:如果什么数据都不回填给textField.text 和 textView. ...

  8. AES && DES加解密

    MD5加密一般不可逆,只能暴力突破.所以这边记录一下一些关于字符串的加解密的两种方法,以便自己学习 AES public class AESHelper { public static string ...

  9. Spring Boot (21) 使用Swagger2构建restful API

    使用swagger可以与spring mvc程序配合组织出强大的restful api文档.它既可以减少我们创建文档的工作量,同时说明内容又整合入现实代码中,让维护文档和修改代码整合为一体,可以让我们 ...

  10. 仿QQ空间长图效果简易版--母亲节感恩

    手机网站 母亲节最火的两件事 1.NBA 杜兰特在获MVP催泪致辞献给母亲:她才是真的MVP. 2.QQ空间长图 ------------------------------------------- ...