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

                            

     

     

  • 相关阅读:
    ubuntu16.04设置开机自启服务
    Flask架构管理及特点(重要)
    Django开发框架知识点
    Django进行数据迁移时,报错:(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)' at line 1")
    Java 网络编程(二) 两类传输协议:TCP UDP
    Java 网络编程(一) 网络基础知识
    .NET开源项目
    手写ArrayList、LinkedList
    命令查看java的class字节码文件
    dubbo总结
  • 原文地址:https://www.cnblogs.com/cshg/p/5723375.html
Copyright © 2011-2022 走看看