zoukankan      html  css  js  c++  java
  • [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 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

    字符匹配问题:
    按着题目要求写就好了
    记住要相反方向进行两次求值
    #include<iostream>
    #include<string>
    using namespace std;
    
    int appx(string& word1,string&word2)
    {
    	int count = 0;
    	int max = 0;
    
    	int length1 = word1.length();
    	int length2 = word2.length();
    
    	for (int i = 0; i<length1; i++)
    	{
    		count = 0;
    		for (int j = 0; j<length2&&i + j<length1; j++)
    		{
    			if (word1[i + j] == word2[j])
    				count++;
    		}
    		if (max<count)
    			max=count;
    	}
    
    	return max;
    }
    
    int main()
    {
    	string word1;
    	string word2;
    
    	while (cin >> word1&&word1 != "-1")
    	{
    		cin >> word2;
    		int len1 = word1.length();
    		int len2 = word2.length();
    
    		int app1 = appx(word1,word2);
    		int app2 = appx(word2,word1);
    
    		if (app1<app2)app1 = app2;
    
    		cout << "appx(";
    		for (int i = 0; i<len1; i++)
    			cout << word1[i];
    		cout << ",";
    		for (int i = 0; i<len2; i++)
    			cout << word2[i];
    		cout << ") = ";
    
    		if (app1 == 0)cout << 0 << endl;
    		else
    		{
    			len1 += len2;
    			app1 *= 2;
    			for (int i = 2; i <= ((len1<app1) ? len1 : app1); i++)
    				while (app1%i == 0 && len1%i == 0)
    				{
    					app1 /= i;
    					len1 /= i;
    				}
    			
    			if (app1%len1 != 0)
    				cout << app1 << '/' << len1 << endl;
    			else
    				cout << app1 / len1 << endl;
    		}
    	}
    
    	return 0;
    }
    

      

  • 相关阅读:
    HTML实现“摇一摇”效果,比较好的两篇文章;
    mongodb查询关于大于小于的用法;
    thenjs的应用
    js原生forEach、map与jquery的each、$.each的区别
    nodejs的url模块中的resolve()的用法总结
    2021.1.22 刷题(环形链表)
    2021.1.21 刷题(定义链表)
    2021.1.21 刷题(移除链表元素)
    2021.1.20 刷题(螺旋矩阵)
    滑动窗口-长度最小的子数组
  • 原文地址:https://www.cnblogs.com/KennyRom/p/6018013.html
Copyright © 2011-2022 走看看