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 ...
随机推荐
- kafka命令大全
kafka命令大全 http://orchome.com/454
- [UE4]Input Key Selector
一.Input Key Selector:按键设置 二.On Key Selected:按键收集完毕后触发该事件. 三.点击On Key Selected控件后,会显示“...”,表示可以接受键盘输入 ...
- udev example -- detect usb and write test file
之前学习了下Udev,就随便做了个测试小程序.....设计什么的也没考虑,就实现了一个基本功能,插入U盘,识别,循环检测到有特定文件后,就然后往U盘里面写数据,插拔多次,都能正常工作. 里面的warn ...
- ssh免密钥之上厕所
ssh服务简单介绍 SSH协议框架中最主要的部分是三个协议: *传输层协议(The Transport Layer Protocol)提供服务器认证,数据机密性,信息完整性等的支持; *用户认证协议( ...
- JS-Promise笔记
转自:http://www.runoob.com/w3cnote/javascript-promise-object.html ECMAscript 6 原生提供了 Promise 对象. Promi ...
- ETL讲解(很详细!!!)
ETL讲解(很详细!!!) ETL是将业务系统的数据经过抽取.清洗转换之后加载到数据仓库的过程,目的是将企业中的分散.零乱.标准不统一的数据整合到一起,为企业的决策提供分析依据. ETL是BI项目重要 ...
- html常见的块元素和行内元素(特别注意个别块元素不能嵌套其他块元素)
html中常见的块元素:div.p.h1-h6.ul.ol.li.hr.table.pre等 块级元素新开启一行即使是设置了width属性也是独占一行(可设置float浮动属性调整布局).尽可能撑满父 ...
- shell数组的使用
定义: array=(1 2 3) echo ${array[0]} echo ${array[1]} echo ${array[2]} echo ${array[*]} 所有元素 echo $ ...
- 一个判断I2C总线通信异常原因的方法
此问题由某客户提出,应用处理器 AP与 MCU进行 I2C通信,通信会经常发生异常,需要定位原因. 首先需要定位的是因为哪个器件发的波形不正确导致通信异常,所以我们在 I2C 线路上增加了以下处理,增 ...
- mysql 乐观锁实现
一.为什么需要锁(并发控制)? 在多用户环境中,在同一时间可能会有多个用户更新相同的记录,这会产生冲突.这就是著名的并发性问题. 典型的冲突有: 1.丢失更新:一个事 ...