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

                            

     

     

  • 相关阅读:
    iOS 获取内外网ip
    iOS 查看层级关系以及调用堆栈
    CoreML Use of undeclared type & Use of unresolved identifier
    AFN的实时网络监控 但是block连续调用了两次
    iOS 11 偏好设置(NSUserDefaults)无效了?
    iOS 11 UIScrollView的新特性(automaticallyAdjustsScrollViewInsets 不起作用了)
    Xcode9~iOS11初体验 无线调试
    Hook~iOS用钩子实现代码注入(埋点方案)
    tomcat启动时端口占用的问题怎么解决
    Memcached在Linux环境下的使用详解http://blog.51cto.com/soysauce93/1737161
  • 原文地址:https://www.cnblogs.com/cshg/p/5723375.html
Copyright © 2011-2022 走看看