我正在阅读一个代码,它在c ++中表示为地图上的键14是否有类似的东西代表一个键,如果有人知道请提前回复
string 1 +“|”是什么意思 +字符串2
这是代码......
#include <iostream>
#include <climits>
#include <unordered_map>
#include <algorithm>
using namespace std;
// N x N matrix
#define N 5
// create a map to store solutions of subproblems
unordered_map<string, string> lookup;
// Function to check if cell (i, j) is a valid cell or not
bool isValid(int i, int j)
{
return (i >= 0 && i < N && j >= 0 && j < N);
}
// Find longest path starting from cell (i, j) formed by adjacent
// numbers in the matrix
string findLongestPath(int mat[N][N], int i, int j)
{
// if the cell is invalid
if (!isValid (i, j))
return 0;
// construct a unique map key from dynamic elements of the input
string key = to_string(i) + "|" + to_string(j);
// if sub-problem is seen for the first time, solve it and
// store its result in a map
if (lookup.find(key) == lookup.end())
{
// string to store path starting (i, j)
string path;
// recurse top cell if its value is +1 of value at (i, j)
if (i > 0 && mat[i - 1][j] - mat[i][j] == 1)
path = findLongestPath(mat, i - 1, j);
// recurse right cell if its value is +1 of value at (i, j)
if (j + 1 < N && mat[i][j + 1] - mat[i][j] == 1)
path = findLongestPath(mat, i, j + 1);
// recurse bottom cell if its value is +1 of value at (i, j)
if (i + 1 < N && mat[i + 1][j] - mat[i][j] == 1)
path = findLongestPath(mat, i + 1, j);
// recurse left cell if its value is +1 of value at (i, j)
if (j > 0 && mat[i][j - 1] - mat[i][j] == 1)
path = findLongestPath(mat, i, j - 1);
// note that as matrix contains all distinct elements,
// there is only one path possible from current cell
lookup[key] = to_string(mat[i][j]) + " - " + path;
}
// return path starting from (i, j)
return lookup[key];
}
发布于 2018-12-07 14:42:55
string1 = "hello";
string2 = "world";
string1 + "|" + string2 == "hello|world"
https://stackoverflow.com/questions/-100003025
复制相似问题