【ACM】hdu_zs3_1005_String Matching_201308100920
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的更多相关文章
- 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”
按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...
- 【ACM】HDU1008 Elevator 新手题前后不同的代码版本
[前言] 很久没有纯粹的写写小代码,偶然想起要回炉再来,就去HDU随便选了个最基础的题,也不记得曾经AC过:最后吃惊的发现,思路完全不一样了,代码风格啥的也有不小的变化.希望是成长了一点点吧.后面定期 ...
- 【ACM】魔方十一题
0. 前言打了两年的百度之星,都没进决赛.我最大的感受就是还是太弱,总结起来就是:人弱就要多做题,人傻就要多做题.题目还是按照分类做可能效果比较好,因此,就有了做几个系列的计划.这是系列中的第一个,解 ...
- 【ACM】那些年,我们挖(WA)过的最短路
不定时更新博客,该博客仅仅是一篇关于最短路的题集,题目顺序随机. 算法思想什么的,我就随便说(复)说(制)咯: Dijkstra算法:以起始点为中心向外层层扩展,直到扩展到终点为止.有贪心的意思. 大 ...
- 【ACM】不要62 (数位DP)
题目:http://acm.acmcoder.com/showproblem.php?pid=2089 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer).杭州交通管理局经常会扩充一些的士车牌照,新 ...
- 【Acm】八皇后问题
八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题. 其解决办法和我以前发过的[算法之美—Fire Net:www.cnblogs.com/lcw/p/3159414.html]类似 题目:在8 ...
- 【ACM】hud1166 敌兵布阵(线段树)
经验: cout 特别慢 如果要求速度 全部用 printf !!! 在学习线段树 内容来自:http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/24 ...
- 【acm】杀人游戏(hdu2211)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2211 杀人游戏 Time Limit: 3000/1000 MS (Java/Others) M ...
- 【ACM】How many prime numbers
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2§ionid=1&problemid=2 #inclu ...
随机推荐
- local_response_normalization 和 batch_normalization
Normalization Normalization local_response_normalization local_response_normalization出现在论文”ImageNe ...
- POJ 2728(最优比率生成树+01规划)
Dese ...
- springboot的登录拦截机制
转自:https://blog.csdn.net/qq_26555463/article/details/78296103 如果是一个后台的管理项目的,有些东西是不能直接就可以访问的,必须要登录才可以 ...
- Labeling Balls(拓扑)
http://poj.org/problem?id=3687 看题意看了半天没看懂怎么回事,看完Discuss彻底凌乱了..后来看了题解才懂,就是逆向建图+拓扑排序,建图时要判重边. #include ...
- go-swagger的简单使用
一.下载go-swagger go-swagger 官方下载 根据不同个的操作系统选择对应的 二.添加环境变量 2.1 window swagger_windows_amd64.exe 将swagge ...
- BZOJ 3876 有上下界的网络流
思路: 套用有上下界的网络流 就好了 (这算是裸题吧) 比如 有条 x->y 的边 流量上限为R 下限为L 那么du[x]-=L,du[y]+=L 流量上限变成R-L du[x]>0 ...
- Jenkins自动化部署.netcore程序
一.安装jenkins 百度一下 二.构建前的准备 搭建好.net core2.0的环境,下载:https://aka.ms/dotnetcore-2-windowshosting (,net co ...
- 一张图说明DIV盒子距离
虚线的宽高为你实际指定的width和height 虚线外的白色区域为padding 红色区域为border的width 红色外的区域为margin
- 【Arduino】LCD 1602 转接板 的默认接线
原来的1602屏需要7个IO口才能驱动起来LCD 1602转接板可以帮你省5个IO口. 在Arduino中,LCD 1602 转接板可以使用函数库LiquidCrystal_I2C1602: 该函数的 ...
- 安卓代码迁移:ActionBarActivity: cannot be resolved to a type
参考链接:http://stackoverflow.com/questions/18830736/actionbaractivity-cannot-be-resolved-to-a-type in e ...