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;
}

时间: 2024-10-25 02:28:50

UVA 10100Longest Match的相关文章

UVA 10100- Longest Match(dp之最长公共子序列)

题目地址:UVA 10100 题意:求两组字符串中最大的按顺序出现的相同单词数目. 思路:将字串中的连续的字母认作一个单词,依次计算出两个字符串中的单词,其中第1个字符串的单词序列为t1.word[1]-..t1.word[n],第2个字符串的单词序列为t2.word[1]-..t2.word[m].然后将每个单词当成一个字符,使用LCS算法计算出两个字符串的最长公共子序列,该序列的长度就是最长匹配. #include <stdio.h> #include <math.h> #in

UVA 10100 Longest Match

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1041 LCS类型的题,不过并不是找common character,而是common word.就先把string处理成a list of word,然后再用LCS算法求common word. 代码如下: 1 #include <iostr

uva:10340 - All in All(字符串匹配)

题目:10340 - All in All 题目大意:给出字符串s和t,问s是否是t的子串.s若去掉某些字符能和t一样,那么t是s的子串. 解题思路:匹配字符.t的每个字符和s中的字符匹配.注意这里的字符数组大小要开大点. 代码: #include <stdio.h> #include <string.h> const int N = 1000005; char s[N], t[N]; bool match () { int i = 0; int lens = strlen(s);

uva 11419 SAM I AM (最小覆盖 K&#246;nig定理)

uva 11419 SAM I AM 题目大意:给出一个R×C的网格,网格上棉纺了一些目标.可以在网格外发射子弹,子弹会沿着垂直或水平方向飞行,并且打掉飞行路径上的所有目标.你的任务是计算出最少需要多少子弹,各从哪个位置发射,才能把所有目标全部打掉. 解题思路:K?nig定理:最小覆盖数等于最大匹配数.把目标所在的坐标,转化为XY结点,行看成X结点,列看成Y结点.那现在问题就变成了,如何选最少的结点,覆盖所有的边. 求最小覆盖的步骤大致如下:1)在右边找到一个未被匹配过的点,标记.2)走一条没被

uva 11383 Golden Tiger Claw (KM算法)

uva 11383 Golden Tiger Claw 题目大意:给定一个N×N的矩阵,每个格子里都有一个正整数w(i,j).你的任务是给每行确定一个整数row(i), 每列也确定一个整数col(i),使得对于格子(i,j),w(i,j)<=row(i)+col(j).所有row(i)和col(j)的总和最小. 解题思路:KM算法. #include <cstdio> #include <cstring> #include <algorithm> #include

UVa OJ 127 - &quot;Accordian&quot; Patience (“手风琴”纸牌)

UVa OJ 127 - "Accordian" Patience ("手风琴"纸牌) Time limit: 3.000 seconds 限时:3.000秒 Problem 问题 You are to simulate the playing of games of "Accordian" patience, the rules for which are as follows: 模拟玩一个"手风琴"纸牌游戏,规则如下: D

UVA 之10010 - Where&#39;s Waldorf?

 Where's Waldorf?  Given a m by n grid of letters, ( ), and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid. A word can match the letters in the grid

UVA 1201 - Taxi Cab Scheme(二分图匹配+最小路径覆盖)

UVA 1201 - Taxi Cab Scheme 题目链接 题意:给定一些乘客,每个乘客需要一个出租车,有一个起始时刻,起点,终点,行走路程为曼哈顿距离,每辆出租车必须在乘客一分钟之前到达,问最少需要几辆出租车 思路:如果一辆车载完一个乘客a,能去载乘客b,就连一条有向边,这样做完整个图形成一个DAG,然后要求的最少数量就是最小路径覆盖,利用二分图最大匹配去做,把每个点拆成两点,如果有边就连边,做一次最大匹配,n - 最大匹配数就是答案 代码: #include <cstdio> #inc

UVA 489-- Hangman Judge--暴力串处理

 Hangman Judge  In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follo