HDU1306 String Matching 【暴力】

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;
}
时间: 2024-08-07 04:11:41

HDU1306 String Matching 【暴力】的相关文章

String Matching -- Brute Force + Rabin-Karp + KMP

String Matching 这个问题已经被做烂了... 下面是C语言实现集合. http://www-igm.univ-mlv.fr/~lecroq/string/ 留个爪- 暴力解法: 暴力美啊- """ Programmer : EOF Date : 2015.02.28 Code file : nsm.py """ def naive_string_matcher(T, P) : if (T or P) is None : return

NYOJ 5 Binary String Matching (kmp 字符串匹配)

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

poj 1580 String Matching(比较字符串的相似程度,四个for循环即可)

String Matching Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3717   Accepted: 1913 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

LeetCode | 1408. String Matching in an Array数组中的字符串匹配【Python】

LeetCode 1408. String Matching in an Array数组中的字符串匹配[Easy][Python][字符串] Problem LeetCode Given an array of string words. Return all strings in words which is substring of another word in any order. String words[i] is substring of words[j], if can be o

[POJ] String Matching

String Matching Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4074   Accepted: 2077 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

九度OJ 1094 String Matching

题目1094:String Matching 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1098 解决:587 题目描述: Finding all occurrences of a pattern in a text is a problem that arises frequently in text-editing programs. Typically,the text is a document being edited,and the pattern searched

NYOJ 5 Binary String Matching【string find的运用】

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

NYOJ5 Binary String Matching

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘100111011

NYOJ题目5---Binary String Matching

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011