我正在参加编程竞赛,在该竞赛中,我将针对每个问题选择使用Python还是C++,因此我愿意使用任何一种语言(无论哪种语言最适合该问题)进行解决方案。
我遇到的过去问题的URL是http://progconz.elena.aut.ac.nz/attachments/article/74/10%20points%20Problem%20Set%202012.pdf,问题F(“ map ”)。
基本上,它涉及将一小块ASCII艺术作品匹配到一个大的艺术作品中。在C++中,我可以为每个ASCII艺术作品制作一个 vector 。问题是当较小的部分是多行时如何匹配它。
我不知道该怎么做。我不想为我编写所有代码,而只是想知道问题所需要的逻辑。
谢谢你的帮助。
到目前为止,这是我得到的:
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main( int argc, char** argv )
{
int nScenarios, areaWidth, areaHeight, patternWidth, patternHeight;
cin >> nScenarios;
for( int a = 0; a < nScenarios; a++ )
{
//get the pattern info and make a vector
cin >> patternHeight >> patternWidth;
vector< vector< bool > > patternIsBuilding( patternHeight, vector<bool>( patternWidth, false ) );
//populate data
for( int i = 0; i < patternHeight; i++ )
{
string temp;
cin >> temp;
for( int j = 0; j < patternWidth; j++ )
{
patternIsBuilding.at( i ).at( j ) = ( temp[ j ] == 'X' );
}
}
//get the area info and make a vector
cin >> areaHeight >> areaWidth;
vector< vector< bool > > areaIsBuilding( areaHeight, vector<bool>( areaWidth, false ) );
//populate data
for( int i = 0; i < areaHeight; i++ )
{
string temp;
cin >> temp;
for( int j = 0; j < areaWidth; j++ )
{
areaIsBuilding.at( i ).at( j ) = ( temp[ j ] == 'X' );
}
}
//now the vectors contain a `true` for a building and a `false` for snow
//need to find the matches for patternIsBuilding inside areaIsBuilding
//how?
}
return 0;
}
编辑:从下面的评论中,我从
J.F. Sebastian
获得了Python中的解决方案。它有效,但我不了解所有情况。我已经评论了我可以做的,但是需要帮助您了解return
函数中的count_pattern
语句。#function to read a matrix from stdin
def read_matrix():
#get the width and height for this matrix
nrows, ncols = map( int, raw_input().split() )
#get the matrix from input
matrix = [ raw_input() for _ in xrange( nrows ) ]
#make sure that it all matches up
assert all(len(row) == ncols for row in matrix)
#return the matrix
return matrix
#perform the check, given the pattern and area map
def count_pattern( pattern, area ):
#get the number of rows, and the number of columns in the first row (cause it's the same for all of them)
nrows = len( pattern )
ncols = len( pattern[0] )
#how does this work?
return sum(
pattern == [ row[ j:j + ncols ] for row in area[ i:i + nrows ] ]
for i in xrange( len( area ) - nrows + 1 )
for j in xrange( len( area[i] ) - ncols + 1 )
)
#get a number of scenarios, and that many times, operate on the two inputted matrices, pattern and area
for _ in xrange( int( raw_input() ) ):
print count_pattern( read_matrix(), read_matrix() )
最佳答案
#how does this work?
return sum(
pattern == [ row[ j:j + ncols ] for row in area[ i:i + nrows ] ]
for i in xrange( len( area ) - nrows + 1 )
for j in xrange( len( area[i] ) - ncols + 1 )
)
可以使用显式的for循环块重写生成器表达式:
count = 0
for i in xrange( len( area ) - nrows + 1 ):
for j in xrange( len( area[i] ) - ncols + 1 ):
count += (pattern == [ row[ j:j + ncols ]
for row in area[ i:i + nrows ] ])
return count
比较(
pattern == ..
)返回True/False,在Python中等于1/0。可以优化构建子矩阵以与模式进行比较的列表理解,以使其更早返回:
count += all(pattern_row == row[j:j + ncols]
for pattern_row, row in zip(pattern, area[i:i + nrows]))
或使用显式的for循环块:
for pattern_row, row in zip(pattern, area[i:i + nrows]):
if pattern_row != row[j:j + ncols]:
break # no match (the count stays the same)
else: # matched (no break)
count += 1 # all rows are equal
关于c++ - 如何在ASCII艺术作品中匹配ASCII艺术作品片段?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17386458/