Algorithm Practice for 1580

String Matching
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 3266 Accepted: 1691

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
 
Solution:
package id1580;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

public class StringMatching {
	BufferedReader inStr = null;
	LinkedList<LinkedList<char[]>> strList = null;
	public StringMatching(){
		inStr = new BufferedReader(new InputStreamReader(System.in));
		strList = new LinkedList<LinkedList<char[]>>();

	}
	private void Start() throws IOException{
		String lineStr = inStr.readLine();
		String[] strArry = new String[2];
		char[] charArry = null;
		LinkedList<char[]> charList = null;
		while(lineStr != null && !lineStr.startsWith("-1")){
			charList = new LinkedList<char[]>();
			charArry = new char[128];
			lineStr = lineStr.trim();
			strArry = lineStr.split(" ");
			
			for(int i = 0; i < strArry[0].length(); i++)
				try{
				charArry[i] = strArry[0].charAt(i);
				//System.err.print(charArry[i]);
				}catch(Exception e){
					System.err.println("strArry[0]: " + strArry[0]);
					System.err.println(i);
				}
			
			charArry = strArry[0].toCharArray();
			//System.out.print("\ninput: " + strArry[0] + " toCharArray " + charArry + '	');
			charList.offer(charArry);
			charArry = strArry[1].toCharArray();
			//System.out.println(strArry[1] + " toCharArray " + strArry[1].length() + strArry[1].toCharArray().toString());
			charList.offer(charArry);
			strList.offer(charList);
			lineStr = inStr.readLine();
		}
		while(!strList.isEmpty()){
			lineMatching(strList.poll());
		}
	}
	private void lineMatching(LinkedList<char[]> charList){
		char[] wordA = charList.poll();
		char[] wordB = charList.poll();
		int matchedCount = 0;
		for(int i = 0; i < wordA.length; i++)
			for(int j = i; j < (Math.min(wordA.length,wordB.length)); j++){
				if(wordA[j] == wordB[j - i])
					matchedCount++;
			}
		for(int i = 1; i < wordB.length; i++)
			for(int j = i; j < (Math.min(wordA.length,wordB.length)); j++){
				if(wordB[j] == wordA[j - i])
					matchedCount++;
			}
		int gcd = getGCD((matchedCount * 2),(wordA.length + wordB.length));
		if (gcd == 0)
			System.out.println("appx(" + new String(wordA)+ "," + new String(wordB) + ") = " + "0");
		else if(gcd == -1)
			System.out.println("appx(" + new String(wordA)+ "," + new String(wordB) + ") = " + "1");
		else 
			System.out.println("appx(" + new String(wordA)+ "," + new String(wordB) + ") = " + (matchedCount * 2) / gcd +  '/' + (wordA.length + wordB.length) / gcd);
	}
	private int getGCD(int a, int b){
		int minuend,subtrahend;
		if (a < b){
			minuend = b;
			subtrahend = a;
		} else if(a > b){
			minuend = a;
			subtrahend = b;
		} else
			return -1;
		if (subtrahend == 0)
			return 0;
		while(subtrahend != minuend){
			if(minuend < subtrahend)
				return minuend;
			minuend = minuend - subtrahend;
		}
		return minuend;
	}
	public static void main(String[] args) {
		try {
			new StringMatching().Start();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值