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 ...
随机推荐
- 4.IIC总线
一.IIC总线说明: IIC总线时序只有高低电平的持续时间一般是大于多少us/ms. iic时序: 开始:当SCL为高电平时,SDA由高电平状态切换到低电平状 ...
- es的返回数据结构
ES即简单又复杂,你可以快速的实现全文检索,又需要了解复杂的REST API.本篇就通过一些简单的搜索命令,帮助你理解ES的相关应用.虽然不能让你理解ES的原理设计,但是可以帮助你理解ES,探寻更多的 ...
- 转:细说ASP.NET Windows身份认证
转自:https://www.cnblogs.com/fish-li/archive/2012/05/07/2486840.html 细说ASP.NET Windows身份认证 阅读目录 开始 认识A ...
- vue打包
npm installnpm run devnpm run build @types/node npm install --save @types/node npm install @types/no ...
- Xilinx------BUFG,IBUFG,BUFGP,IBUFGDS等含义以及使用
转载-----BUFG,IBUFG,BUFGP,IBUFGDS等含义以及使用 目前,大型设计一般推荐使用同步时序电路.同步时序电路基于时钟触发沿设计,对时钟的周期.占空比.延时和抖动提出了更高的要 ...
- 第一章 FFmpeg简介
1.1 FFmpeg的定义 Fast Forward Moving Picture Experts Group(动态图像专家组) 1.2 FFmpeg的历史 1.3 FFmpeg的基本组成 1 FFm ...
- Java笔试面试题整理第六波(修正版)
转载至:http://blog.csdn.net/shakespeare001/article/details/51330745 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- mongo官方企业版安装及数据库授权使用
通过安装.deb包的方式,系统是Ubuntu 16.04 1. Import the public key used by the package management system.(导入包管理系统 ...
- RabbitMQ--windows10环境下的RabbitMQ安装步骤(转)
https://blog.csdn.net/weixin_39735923/article/details/79288578
- python调用cmd显示中文乱码及调用cmd命令
os.system('dir') 解决方法加上 os.system('chcp 65001') ____________________________________________________ ...