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 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

Pacific Northwest 1999

思路:

就是简单地 一个暴力遍历。

体会:

细节很重要,团队默契很重要。因为与队友的对题意理解的分歧,导致这道水题到最后愣是没有AC出来。虽说最后是我的理解正确,但是因为这个分歧,直接影响了团队配合的默契,还是要提高自身能力,提高团队配合力度。心向往之,行而不止。加油!

代码如下:

#include<stdio.h>
#include<string.h>
int gcd(int a,int b)
{
	return !b?a:gcd(b,a%b);
}
char a[1010],b[1010],temp[2022];
int main()
{
	int i,j,i1,j1;
	while(~scanf("%s",a),strcmp(a,"-1"))
	{
		scanf("%s",b);
		int len1=strlen(a);
		int len2=strlen(b);
		int max=0;
		for(i=0;i<len1;i++)
		{
			for(j=0;j<len2;j++)
			{
				int m=0;
				for(i1=i,j1=j;i1<len1&&j1<len2;i1++,j1++)
				{
					if(a[i1]==b[j1])
					++m;
				}
				if(max<m)
				max=m;
			}
		}
		if(max==0)
		printf("appx(%s,%s) = 0\n",a,b);
		else
		{
			int s;
			s=len1+len2;
			max*=2;
			if(s==(max))
			printf("appx(%s,%s) = 1\n",a,b);
			else
			{
				int f;
				f=gcd(s,max);
				printf("appx(%s,%s) = %d/%d\n",a,b,max/f,s/f);
			}
		}
	}
	return 0;
}
时间: 2024-10-13 07:04:06

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

poj 1580 String Matching 【字符串处理】

/*题意:移动字符串一与另外的一个字符串匹配,找最多的匹配个数 策略  暴力  就是将一个字符串固定,然后用另一个字符串从左往右来跟这一字符串来比较*/ #include <stdio.h> #include <string.h> #define M 10000 char a[M], b[M]; int gcd(int a, int b) { if(b == 0) return a; else return gcd(b, a%b); } int main() { while(sca

[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

poj 3077 Rounders 【简单字符串处理】

题意:就是4舍5入到最近的数. 题意有些难理解... 代码: #include <stdio.h> #include <string.h> char s[10]; int main() { int t, n; scanf("%d", &t); while(t --){ memset(s, 0, sizeof(s)); scanf("%s", s); int len = strlen(s); if(len == 1){ printf(&

POJ 1200 Crazy Search (字符串hash)

题目大意: 分析长度为n的子串有多少种. 思路分析: 对于没出现的字符,将其分配一个数字. 然后将子串看做一个nc进制的数. 然后hash判断. #include <cstdio> #include <iostream> #include <algorithm> #include <map> #include <cstring> #include <string> using namespace std; bool vis[26666

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

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 3617 Best Cow Line (字符串反转贪心算法)

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 2826 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his

POJ 2406 Power Strings (求字符串循环节出现的次数)

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 44217   Accepted: 18449 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "

KMP String Matching Algorithm

This is a program based on Knuth-Morris-Pratt String Matching Algorithm (a.k.a KMP algorithm), which can solve the problem POJ 3461. 1 import java.io.*; 2 import java.util.*; 3 4 class Input { 5 private Scanner in; 6 private StringTokenizer tok; 7 8