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 <iostream>
  2 #include <math.h>
  3 #include <stdio.h>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <string.h>
  7 #include <cstring>
  8 #include <queue>
  9 #include <vector>
 10 #include <functional>
 11 #include <cmath>
 12 #define SCF(a) scanf("%d", &a)
 13 #define IN(a) cin>>a
 14 #define FOR(i, a, b) for(int i=a;i<b;i++)
 15 typedef long long Int;
 16 using namespace std;
 17
 18 int main()
 19 {
 20     char str1[1005], str2[1005];
 21     vector<string> v1, v2;
 22     int testCase = 1;
 23     int len1 = 0, len2 = 0;
 24     while (cin.getline(str1, 1005))
 25     {
 26         cin.getline(str2, 1005);
 27         int cnum = 0;
 28         char word[25];
 29         string wd;
 30         len1 = 0;
 31         len2 = 0;
 32         for (int i = 0; str1[i] != ‘\0‘; i++)
 33         {
 34             len1++;
 35             if ((str1[i] >= ‘a‘ && str1[i] <= ‘z‘) || (str1[i] >= ‘A‘ && str1[i] >= ‘Z‘) || (str1[i] >= ‘0‘ && str1[i] <= ‘9‘))
 36             {
 37                 word[cnum++] = str1[i];
 38             }
 39             else
 40             {
 41                 if (cnum > 0)
 42                 {
 43                     word[cnum++] = ‘\0‘;
 44                     wd = string(word);
 45                     v1.push_back(wd);
 46                 }
 47                 cnum = 0;
 48             }
 49         }
 50         if (cnum > 0)
 51         {
 52             word[cnum++] = ‘\0‘;
 53             wd = string(word);
 54             v1.push_back(wd);
 55         }
 56         cnum = 0;
 57         for (int i = 0; str2[i] != ‘\0‘; i++)
 58         {
 59             len2++;
 60             if ((str2[i] >= ‘a‘ && str2[i] <= ‘z‘) || (str2[i] >= ‘A‘ && str2[i] >= ‘Z‘) || (str2[i] >= ‘0‘ && str2[i] <= ‘9‘))
 61             {
 62                 word[cnum++] = str2[i];
 63             }
 64             else
 65             {
 66                 if (cnum > 0)
 67                 {
 68                     word[cnum++] = ‘\0‘;
 69                     wd = string(word);
 70                     v2.push_back(wd);
 71                 }
 72                 cnum = 0;
 73             }
 74         }
 75         if (cnum > 0)
 76         {
 77             word[cnum++] = ‘\0‘;
 78             wd = string(word);
 79             v2.push_back(wd);
 80         }
 81
 82         int **match = new int*[v1.size() + 1];
 83         FOR(i, 0, v1.size() + 1)
 84             match[i] = new int[v2.size() + 1];
 85
 86         FOR(i, 0, v1.size() + 1)
 87             match[i][0] = 0;
 88         FOR(i, 0, v2.size() + 1)
 89             match[0][i] = 0;
 90
 91         FOR(i, 1, v1.size() + 1)
 92         {
 93             FOR(j, 1, v2.size() + 1)
 94             {
 95                 if (v1[i - 1] == v2[j - 1])
 96                     match[i][j] = match[i - 1][j - 1] + 1;
 97                 else
 98                     match[i][j] = max(match[i - 1][j], match[i][j - 1]);
 99             }
100         }
101         if(len1==0 || len2==0)
102             printf("%2d. Blank!\n", testCase++);
103         else
104             printf("%2d. Length of longest match: %d\n", testCase++, match[v1.size()][v2.size()]);
105
106         while (!v1.empty())
107             v1.pop_back();
108         while (!v2.empty())
109             v2.pop_back();
110
111         FOR(i, 0, v1.size() + 1)
112             delete[] match[i];
113         delete[] match;
114
115     }
116     return 0;
117 }
时间: 2024-11-24 07:34:44

UVA 10100 Longest Match的相关文章

uva 10405 Longest Common Subsequence (最长公共子序列)

uva 10405 Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences: abcdgh a

uva 10285 Longest Run on a Snowboard (记忆化搜索)

uva 10285 Longest Run on a Snowboard 题目大意:给出一张n*m的雪地地图,每格标注的是该点的高度.从地势高的地方可以滑到地势低的地方(只能上下左右滑),问最长的滑雪距离. 解题思路:逐一访问各点,若该点没有被访问过,则进行DFS找出该点为滑雪起始点的最长滑雪距离,用dp数组记录,若该点已被访问过,则返回其对应的dp数组记录的值. #include <cstdio> #include <cstring> #include <algorithm

UVA 10405 Longest Common Subsequence

最长公共子系列,简单的dp,不过注意有空格,所以,在读字符串的时候,尽量用gets读,这样基本没问题 #include<iostream> #include<cstdio> #include<string> #include<cstring> using namespace std; int dp[1001][1001]; int MAX(int x,int y) { if (x>y) return x; else return y; } int ma

UVA10100:Longest Match(最长公共子序列)&amp;&amp;HDU1458Common Subsequence ( LCS)

题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两个序列X 和 Y,用i 和 j分别作为它们的前缀指针,f[i][j]表示序列X的前缀Xi 和 序列Y的前缀Yi 的最长公共子序列的长度,在这道题中,可把每一个单词当作一个字符来进行比较. 当 i | j 为0时 ,此 f[i][j] = 0; 当 i!=0 && j!=0 &&

uva 11151 Longest Palindrome (最长公共子序列)

uva 11151 Longest Palindrome A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-p

Uva 11151 - Longest Palindrome

A palindrome is a string that reads the same from the left as it does from the right. For example, I, GAG and MADAM are palindromes, but ADAM is not. Here, we consider also the empty string as a palindrome. From any non-palindromic string, you can al

UVa 10285 Longest Run on a Snowboard(DP 二维最长递减子序列)

题意  输入一个城市的滑雪地图  你可以从高的地方滑到伤下左右低的地方  求这个城市的最长滑雪线路长度   即在一个矩阵中找出最长递减连续序列 令d[i][j]为以格子map(i,j)为起点的最长序列   则有状态转移方程d[i][j]=max{d[a][b]}+1  a,b为与i,j相邻且值比i,j小的所有点 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #def

UVa 10285 Longest Run on a Snowboard

这题我的第一感觉就是用DFS.自己写的貌似不够完美,因为我看见别人的时间都特别的短,而我的有点长. #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> #include<stack> #include<queue> using namespace std; const int N=102; int height[N][N]; bool vi

UVa 10285 Longest Run on a Snowboard【记忆化搜索】

题意:和最长滑雪路径一样, 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<algori