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。
代码如下:
#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的更多相关文章
- UVA10100:Longest Match(最长公共子序列)&&HDU1458Common Subsequence ( LCS)
题目链接:http://blog.csdn.net/u014361775/article/details/42873875 题目解析: 给定两行字符串序列,输出它们之间最大公共子单词的个数 对于给的两 ...
- UVA 10405 Longest Common Subsequence
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=16&p ...
- UVA 10000 Longest Paths (SPFA算法,模板题)
题意:给出源点和边,边权为1,让你求从源点出发的最长路径,求出路径长度和最后地点,若有多组,输出具有最小编号的最后地点. #include <iostream> #include < ...
- UVA 10285 - Longest Run on a Snowboard (记忆化搜索+dp)
Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 seconds Memor ...
- UVA 10285 Longest Run on a Snowboard(记忆化搜索)
Problem C Longest Run on a Snowboard Input: standard input Output: standard output Time Limit: 5 sec ...
- UVa 10285 Longest Run on a Snowboard - 记忆化搜索
记忆化搜索,完事... Code /** * UVa * Problem#10285 * Accepted * Time:0ms */ #include<iostream> #includ ...
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
- 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 ...
- UVA 10405 Longest Common Subsequence --经典DP
最长公共子序列,经典问题.算是我的DP开场题吧. dp[i][j]表示到s1的i位置,s2的j位置为止,前面最长公共子序列的长度. 状态转移: dp[i][j] = 0 ...
随机推荐
- IDEA修改git账号及密码的方法 ----绝壁好使
就这个这个位置,之前的账号信息就保存在这里,直接编辑修改,修改成你现在的账号, idea提交的时候就能直接提交,或者提交的时候要输入账号密码,ok,我的微信 18550918581,不懂再找我
- [UE4]Button
一.按钮有4种状态:Normal(普通状态).Hovered(鼠标悬停状态).Pressed(鼠标按下状态).Disabled(禁用状态),可以分别给每种状态设置样式. 二.按钮有如图所示的5个事件, ...
- webpy 解决中文出现UnicodeDecodeError: 'ascii' codec can't decode byte 问题
1.问题描述:一个在Django框架下使用Python编写的定时更新项目,在Windows系统下测试无误,在Linux系统下测试,报如下错误: ascii codec can't decode byt ...
- Oracle 唯一 约束(unique constraint) 与 索引(index) 关系说明
一. 官网对Unique Constraints说明 http://download.oracle.com/docs/cd/E11882_01/server.112/e16508/datainte.h ...
- python常见循环练习
第一题:求5的阶乘 # 方法1,递归 def jc(num): if num == 1: return 1 else: return num*jc(num-1) print(jc(5)) # 方法2, ...
- 【学习】数据处理基础知识(基本功能)【pandas】
本章介绍pandas的重要功能,只记录一些重点内容 1.重新索引 pandas对象的一个重要方法是reindex,其作用是创建一个适应用新索引的新对象 #重新索引 obj = pd.Series([4 ...
- threading.local()源码分析
前段时间写了个多线程的程序,了解到Python中有个与众不同的thread.local()方法,可以创建一个全局对象,各个线程可以用这个全局对象保存各自的局部变量,而在使用时不受其他线程的影响.于是抽 ...
- MySQL存储过程定义中的特性(characteristic)的含义
MySQL的存储过程蛮啰嗦的,与MSSQL或者Oracle的存储过程相比,如果没有显式指定,他会隐含地指定一系列特性(characteristic)的默认值来创建存储过程 通常在使用图形界面工具进行存 ...
- SQL Server中使用数据库快照的方式来完成测试环境中数据库的轻量级备份还原操作
在开发或者测试环境的数据库中,经常会发现有开发或者测试人员误删除表或者数据的情况,对于开发或者测试库,一般都没有安排定时的备份任务去备份数据库,一方面是由于存储资源有限,不太可能给开发或者测试环境准备 ...
- C++ queue
queuequeue 模板类的定义在<queue>头文件中.与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选 ...