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 ...
随机推荐
- 【算法和数据结构】_16_小算法_IntToStr: 将整型数据转换为字符串
/* IntToStr: 将整型数据转换为字符串 */ #include <stdio.h> void int_to_str(const unsigned long int i_numbe ...
- 前端-JavaScript1-4——JavaScript之变量
变量(Variables),和高中代数学习的x.y.z很像,它们不是字母,而是蕴含值的符号. 它和直接量不同,直接量5,就是数字5:直接量”你好”就是字符串“你好”.现在这个变量不一样了,你看见一个a ...
- cookie.js插件
/*! cookiejs v1.0.23 | MIT (c) 2018 kenny wong | https://github.com/jaywcjlove/cookie.js */!function ...
- CSS之checkbox&radio&textarea&select
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- [UE4]VR成像原理
一.双眼成像原理 二.3D电影成像原理 模拟人眼.用2个摄像机拍摄,模拟人的左眼和右眼 播放的时候2个投影仪分别同时播放左右摄像机拍摄到内容,观众带上3D眼镜,左眼只能看到左摄像机的内容(过滤右摄像机 ...
- UE4 UMG
转自:https://www.cnblogs.com/kadaj/p/6412937.html 1.创建关卡类 1.创建C++类继承LevelScriptActor 2.打开关卡蓝图 Class Se ...
- FragmentTabHost切换Fragment时保存状态,避免切换Fragment走onCreateView和onDestroyView方法;
FragmentTabHost这个控件每次切换Fragment,都会走Fragment的onCreateView和onDestroyView方法,多以每次切换都会创建和销毁Fragment实例,先来看 ...
- NodeJS client code websocket
var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); client.on('co ...
- 学习笔记:Stage.js(又叫Cut.js)——2D canvas 开发库,游戏方面的
http://piqnt.com/stage.js/ http://www.bootcdn.cn/stage.js/ Stage.js(可能又叫Cut.js)是一个2D HTML5 JavaScrip ...
- IDEA run下output标签悬浮/弹出状态
就是这样,在哪儿有一个恢复布局标签