zoukankan      html  css  js  c++  java
  • UVA 10100Longest Match

     

    Longest Match

    A newly opened detective agency is struggling with their limited intelligence to find out a secret

    information passing technique among its detectives. Since they are new in this profession, they know

    well that their messages will easily be trapped and hence modified by other groups. They want to guess

    the intensions of other groups by checking the changed sections of messages. First they have to get the

    length of longest match. You are going to help them.

    Input

    The input le may contain multiple test cases. Each case will contain two successive lines of string.

    Blank lines and non-letter printable punctuation characters may appear. Each Line of string will be

    no longer than 1000 characters. Length of each word will be less than 20 characters.

    Output

    For each case of input, you have to output a line starting with the case number right justified in a field

    width of two, followed by the longest match as shown in the sample output. In case of at least one blank

    line for each input output `

    Blank!

    Consider the non-letter punctuation characters as white-spaces.

    SampleInput

    This is a test.

    test

    Hello!

    The document provides late-breaking information

    late breaking.

    SampleOutput

    1. Length of longest match: 1

    2. Blank!

    3. Length of longest match: 2

     这个题就是变形的最长公共子序列, 换汤不换药

     最长公共子序列递推公式:

    dp[i+1][j+1] = {

                           max(dp[i][j]+1, d[i][j+1], d[i+1][j])  (Si+1 = tj+1

                          max(d[i][j+1], d[i+1][j]) 其他

                           }

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <cstring>
    #include <cctype>
    using namespace std;
    
    const int maxn = 1003;
    string text[maxn], pattern[maxn];
    int dp[maxn][maxn];
    bool istrue(char c){
        if((c >= 'a' && c <= 'z' )||(c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')){
            return true;
        }
        return false;
    }
    void devide(string str[], string s, int &ans){
        ans = 0;
        str[0].clear();
        for(int i = 0; i < s.size(); i++){
            if(istrue(s[i])){
                str[ans] += s[i];
            }else {
                if(str[ans].empty()){
                    continue;
                }
                ans++;
                str[ans].clear();
            }
        }
        if(istrue(s[s.size()-1])) ans++;
    }
    int main() {
        string str1, str2;
        int num = 0, num1, num2;;
        while(!cin.eof()){
            getline(cin, str1);
            getline(cin, str2);
            printf("%2d. ", ++num);
            if(str1.empty() || str2.empty()){
                cout << "Blank!" << endl;
            }else {
                devide(text, str1, num1);
                devide(pattern, str2, num2);
                memset(dp, 0, sizeof(dp));
                for(int i = 0; i < num1; i++){
                    for(int j = 0; j < num2; j++){
                        if(text[i] == pattern[j]){
                            dp[i+1][j+1] = dp[i][j] + 1;
                        }else {
                            dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
                        }
                    }
                }
                cout << "Length of longest match: "<<dp[num1][num2] << endl;
            }
        }
        return 0;
    }
    View Code

                            

     

     

  • 相关阅读:
    用Doxygen生成X3D的继承关系树
    FreeBSD 8.0候选版本RC2发布
    Mozilla Firefox, Apple Safari,Chrome等主流浏览器均开始WebGL支持
    关于企业管理信息系统
    [转]WebGL标准最新进展
    C++ + Irrlicht整一个东东?
    FreeWRL Windows Beta版本注记
    选择C++开发环境
    老人与老浏览器-李开复与成熟度最高的VRML浏览器SGI Cosmo
    WebGL概念及HTML5推广给X3D规范带来的新出路
  • 原文地址:https://www.cnblogs.com/cshg/p/5723375.html
Copyright © 2011-2022 走看看