题目链接: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。

代码如下:

 #include <iostream>
#include <math.h>
#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <cmath>
#define SCF(a) scanf("%d", &a)
#define IN(a) cin>>a
#define FOR(i, a, b) for(int i=a;i<b;i++)
typedef long long Int;
using namespace std; int main()
{
char str1[], str2[];
vector<string> v1, v2;
int testCase = ;
int len1 = , len2 = ;
while (cin.getline(str1, ))
{
cin.getline(str2, );
int cnum = ;
char word[];
string wd;
len1 = ;
len2 = ;
for (int i = ; str1[i] != '\0'; i++)
{
len1++;
if ((str1[i] >= 'a' && str1[i] <= 'z') || (str1[i] >= 'A' && str1[i] >= 'Z') || (str1[i] >= '' && str1[i] <= ''))
{
word[cnum++] = str1[i];
}
else
{
if (cnum > )
{
word[cnum++] = '\0';
wd = string(word);
v1.push_back(wd);
}
cnum = ;
}
}
if (cnum > )
{
word[cnum++] = '\0';
wd = string(word);
v1.push_back(wd);
}
cnum = ;
for (int i = ; str2[i] != '\0'; i++)
{
len2++;
if ((str2[i] >= 'a' && str2[i] <= 'z') || (str2[i] >= 'A' && str2[i] >= 'Z') || (str2[i] >= '' && str2[i] <= ''))
{
word[cnum++] = str2[i];
}
else
{
if (cnum > )
{
word[cnum++] = '\0';
wd = string(word);
v2.push_back(wd);
}
cnum = ;
}
}
if (cnum > )
{
word[cnum++] = '\0';
wd = string(word);
v2.push_back(wd);
} int **match = new int*[v1.size() + ];
FOR(i, , v1.size() + )
match[i] = new int[v2.size() + ]; FOR(i, , v1.size() + )
match[i][] = ;
FOR(i, , v2.size() + )
match[][i] = ; FOR(i, , v1.size() + )
{
FOR(j, , v2.size() + )
{
if (v1[i - ] == v2[j - ])
match[i][j] = match[i - ][j - ] + ;
else
match[i][j] = max(match[i - ][j], match[i][j - ]);
}
}
if(len1== || len2==)
printf("%2d. Blank!\n", testCase++);
else
printf("%2d. Length of longest match: %d\n", testCase++, match[v1.size()][v2.size()]); while (!v1.empty())
v1.pop_back();
while (!v2.empty())
v2.pop_back(); FOR(i, , v1.size() + )
delete[] match[i];
delete[] match; }
return ;
}

UVA 10100 Longest Match的更多相关文章

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

    题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...

  2. UVA 10405 Longest Common Subsequence

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...

  3. UVA 10000 Longest Paths (SPFA算法,模板题)

    题意:给出源点和边,边权为1,让你求从源点出发的最长路径,求出路径长度和最后地点,若有多组,输出具有最小编号的最后地点. #include <iostream> #include < ...

  4. UVA 10285 - Longest Run on a Snowboard (记忆化搜索+dp)

    Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 seconds Memor ...

  5. UVA 10285 Longest Run on a Snowboard(记忆化搜索)

    Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...

  6. UVa 10285 Longest Run on a Snowboard - 记忆化搜索

    记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...

  7. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  8. 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 ...

  9. UVA 10405 Longest Common Subsequence --经典DP

    最长公共子序列,经典问题.算是我的DP开场题吧. dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度. 状态转移: dp[i][j] = 0                 ...

随机推荐

  1. 【spring】之xml和Annotation,Bean注入的方式

    基于xml形式Bean注入 @Data @AllArgsConstructor @NoArgsConstructor public class PersonBean { private Integer ...

  2. 在他机上还原DB2的备份

    在服务器获取得到db2的备份文件,拷贝到d盘db2_backup目录下面 在windows下的时间戳标记为时间目录名+文件名.001前面的 "2014022\0001006.001" ...

  3. C++Primer第五版——习题答案详解(三)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...

  4. Redis为什么可以支持那么大的并发访问量?为什么redis没有单点并发瓶颈?

    一是redis使用内存 而是redis使用多路复用的IO模型: 现代的UNIX操作系统提供了select/poll/kqueue/epoll这样的系统调用,这些系统调用的功能是:你告知我一批套接字,当 ...

  5. c# 数据结构 ArrayList

    数据结构 描述数据之间的关系 行为:添加数据,删除数据,插入数据,查找数据,修改数据 追加数据:向这个结构的末尾添加一个数据 删除数据:在这个结构中删除你指定的数据 插入数据:向这个结构中某一个位置插 ...

  6. TypeScript type 类型别名

    //6,类型别名 /**类型别名不能出现在声明右侧的任何地方. * 接口 vs. 类型别名 * 另一个重要区别是类型别名不能被extends和implements(自己也不能extends和imple ...

  7. 二、CSS选择器

    1.CSS派生选择器 概念:通过依据元素在其位置的上下文关系来定义样式. 实例: <!--index.html--> <!DOCTYPE html> <html lang ...

  8. [Linux] traceroute 路由跟踪指令用例

    traceroute是用来跟踪数据包到达网络主机所经过的路由工具.在Linux系统中,称之为traceroute,在Windows中称为tracert. 一条路径上的每个设备traceroute要测3 ...

  9. tensorflow实战讨论

    欢迎关注微信公众号:樱园的玻尔兹曼机

  10. Javascript中 toFixed

    javascript中toFixed使用的是银行家舍入规则. 银行家舍入:所谓银行家舍入法,其实质是一种四舍六入五取偶(又称四舍六入五留双)法. 简单来说就是:四舍六入五考虑,五后非零就进一,五后为零 ...