- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
问题:我正在尝试使用 C++ 从游戏名称中提取古代防御 (DotA) 的有效游戏模式。
详细信息:
这里是各种游戏模式的列表,图表显示了每种模式与哪些次要模式兼容(X 表示不兼容):
// Only 1 primary allowed
static char *Primary[] = {
// Compatible with > | dm | rv | mm | du | sh | aa | ai | as | id | em | np | sc | om | nt | nm | nb | ro | mo | sp |
"ap", // All Pick | | | | | | | | | | | | | | | | | | | |
"ar", // All Random | | X | | | | | | | | | | | | | | | | | |
"tr", // Team Random | X | X | | | | | | | | | | | | | | | | | |
"mr", // Mode Random | X | X | | | X | X | X | X | | | | | | | | | X | X | |
"lm", // League Mode | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | |
"rd", // Random Draft | X | X | X | | X | X | X | X | | | | | | | | | X | X | |
"vr", // Vote Random | X | X | X | | X | X | X | X | | | | | | | | | X | X | |
"el", // Extended League | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | |
"sd", // Single Draft | X | X | X | | X | X | X | X | | | | | | | | | X | X | |
"cm", // Captains Mode | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X | X |
"cd" // Captains Draft | X | X | X | | X | X | X | X | | | | | | | | | X | X | |
};
static char *Secondary[] = {
// Compatible with > | dm | rv | mm | du | sh | aa | ai | as | id | em | np | sc | om | nt | nm | nb | ro | mo | sp |
"dm", // Death Match | | X | X | | X | X | X | X | | | | | | | | | X | X | |
"rv", // Reverse Mode | X | | | | X | | | | | | | | | | | | | | |
"mm", // Mirror Match | X | | | | X | | | | | | | | | | | | | | |
"du", // Duplicate Mode | | | | | | | | | | | | | | | | | | | |
"sh", // Same Hero | X | X | X | | | | | | | | | | | | | | | | |
"aa", // All Agility | X | | | | | | X | X | | | | | | | | | | | |
"ai", // All Intelligence | X | | | | | X | | X | | | | | | | | | | | |
"as", // All Strength | X | | | | | X | X | | | | | | | | | | | | |
"id", // Item Drop | | | | | | | | | | | | | | | | | | | |
"em", // Easy Mode | | | | | | | | | | | | | | | | | | | |
"np", // No Powerups | | | | | | | | | | | | | | | | | | | |
"sc", // Super Creeps | | | | | | | | | | | | | | | | | | | |
"om", // Only Mid | | | | | | | | | | | | | | | | | | | |
"nt", // No Top | | | | | | | | | | | | | | | | | | | |
"nm", // No Middle | | | | | | | | | | | | | | | | | | | |
"nb", // No Bottom | | | | | | | | | | | | | | | | | | | |
"ro", // Range Only | X | | | | | | | | | | | | | | | | | X | |
"mo", // Melee Only | X | | | | | | | | | | | | | | | | X | | |
"sp" // Shuffle Players | | | | | | | | | | | | | | | | | | | |
};
// These options are always available
static char *Misc[] = {
"ns", // No Swap
"nr", // No Repick
"ts", // Terrain Snow
"pm", // Pooling Mode
"oi", // Observer Info
"mi", // Mini Heroes
"fr", // Fast Respawn
"so" // Switch On
};
示例:这是一些示例输入,具有所需的输出:
“DotA v6.60 -RDSOSP USA/CA LC!” -> “rdsosp”
“DOTA AREMDM 美国 LC”->“aremdm”
“DotA v6.60 -ApEmDuSpId USA BL”->“apemduspid”
注意:解决方案不一定必须提供实际代码、伪代码,甚至只是对您将如何处理它的解释也是可以接受和首选的。此外,该解决方案需要足够灵活,以便我可以相当轻松地添加另一种游戏模式。还可以安全地假设在游戏名称中,游戏模式将始终以主要游戏模式开头。
结果:
#include <cstdarg>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>
#include <map>
#include <vector>
std::map<std::string, std::vector<std::string> > ModeCompatibilityMap;
static const unsigned int PrimaryModesCount = 11;
static char *PrimaryModes[] = {
"ap", "ar", "tr", "mr", "lm", "rd", "vr", "el", "sd", "cm", "cd"
};
static const unsigned int SecondaryModesCounts = 19;
static char *SecondaryModes[] = {
"dm", "rv", "mm", "du", "sh", "aa", "ai", "as", "id", "em", "np",
"sc", "om", "nt", "nm", "nb", "ro", "mo", "sp"
};
static const unsigned int MiscModesCount = 8;
static char *MiscModes[] = {
"ns", "nr", "ts", "pm", "oi", "mi", "fr", "so"
};
std::vector<std::string> Vectorize( int count, ... ) {
std::vector<std::string> result;
va_list vl;
va_start( vl, count );
for ( int i = 0; i < count; ++i ) {
char *buffer = va_arg( vl, char * );
result.push_back( buffer );
}
va_end( vl );
return result;
}
void InitializeModeCompatibilityMap() {
// Primary
ModeCompatibilityMap["ar"] = Vectorize( 1, "rv" );
ModeCompatibilityMap["tr"] = Vectorize( 2, "dm", "rv" );
ModeCompatibilityMap["mr"] = Vectorize( 8, "dm", "rv", "sh", "aa", "ai", "as", "ro", "mo" );
ModeCompatibilityMap["lm"] = Vectorize( 18, "dm", "rv", "mm", "du", "sh", "aa", "ai", "as", "id", "em", "np", "sc", "om", "nt", "nm", "nb", "ro", "mo" );
ModeCompatibilityMap["rd"] = Vectorize( 9, "dm", "rv", "mm", "sh", "aa", "ai", "as", "ro", "mo" );
ModeCompatibilityMap["vr"] = Vectorize( 9, "dm", "rv", "mm", "sh", "aa", "ai", "as", "ro", "mo" );
ModeCompatibilityMap["el"] = Vectorize( 18, "dm", "rv", "mm", "du", "sh", "aa", "ai", "as", "id", "em", "np", "sc", "om", "nt", "nm", "nb", "ro", "mo" );
ModeCompatibilityMap["sd"] = Vectorize( 9, "dm", "rv", "mm", "sh", "aa", "ai", "as", "ro", "mo" );
ModeCompatibilityMap["cm"] = Vectorize( 19, "dm", "rv", "mm", "du", "sh", "aa", "ai", "as", "id", "em", "np", "sc", "om", "nt", "nm", "nb", "ro", "mo", "sp" );
ModeCompatibilityMap["cd"] = Vectorize( 9, "dm", "rv", "mm", "sh", "aa", "ai", "as", "ro", "mo" );
// Secondary
ModeCompatibilityMap["dm"] = Vectorize( 8, "rv", "mm", "sh", "aa", "ai", "as", "ro", "mo" );
ModeCompatibilityMap["rv"] = Vectorize( 2, "dm", "sh" );
ModeCompatibilityMap["mm"] = Vectorize( 2, "dm", "sh" );
ModeCompatibilityMap["sh"] = Vectorize( 3, "dm", "rv", "mm" );
ModeCompatibilityMap["aa"] = Vectorize( 3, "dm", "ai", "as" );
ModeCompatibilityMap["ai"] = Vectorize( 3, "dm", "aa", "as" );
ModeCompatibilityMap["as"] = Vectorize( 3, "dm", "aa", "ai" );
ModeCompatibilityMap["ro"] = Vectorize( 2, "dm", "mo" );
ModeCompatibilityMap["mo"] = Vectorize( 2, "dm", "ro" );
}
std::vector<std::string> Tokenize( const std::string &string ) {
std::vector<std::string> tokens;
std::string token;
std::stringstream ss( string );
while ( ss >> token ) {
tokens.push_back( token );
}
return tokens;
}
void SanitizeString( std::string &in ) {
std::transform( in.begin(), in.end(), in.begin(), tolower );
for ( size_t i = 0; i < in.size(); ++i ) {
if ( in[i] < 'a' || in[i] > 'z' ) {
in.erase( i--, 1 );
}
}
}
std::vector<std::string> SplitString( const std::string &in, int count ) {
std::vector<std::string> result;
if ( in.length() % count != 0 ) {
return result;
}
for ( std::string::const_iterator i = in.begin(); i != in.end(); i += count ) {
result.push_back( std::string( i, i + count ) );
}
return result;
}
bool IsPrimaryGameMode( const std::string &in ) {
for ( int i = 0; i < PrimaryModesCount; ++i ) {
if ( strcmp( PrimaryModes[i], in.c_str() ) == 0 ) {
return true;
}
}
return false;
}
bool IsSecondaryGameMode( const std::string &in ) {
for ( int i = 0; i < SecondaryModesCounts; ++i ) {
if ( strcmp( SecondaryModes[i], in.c_str() ) == 0 ) {
return true;
}
}
return false;
}
bool IsMiscGameMode( const std::string &in ) {
for ( int i = 0; i < MiscModesCount; ++i ) {
if ( strcmp( MiscModes[i], in.c_str() ) == 0 ) {
return true;
}
}
return false;
}
bool IsValidGameMode( std::string in, std::string &out ) {
// 1. Strip all non-letters from the string and convert it to lower-case
SanitizeString( in );
// 2. Confirm that it is a multiple of 2
if ( in.length() == 0 || in.length() % 2 != 0 ) {
return false;
}
// 3. Split the string further into strings of 2 characters
std::vector<std::string> modes = SplitString( in, 2 );
// 4. Verify that each game mode is a valid game mode and is compatible with the others
bool primaryModeSet = false;
for ( size_t i = 0; i < modes.size(); ++i ) {
if ( IsPrimaryGameMode( modes[i] ) || IsSecondaryGameMode( modes[i] ) ) {
if ( IsPrimaryGameMode( modes[i] ) ) {
if ( primaryModeSet ) {
return false;
}
primaryModeSet = true;
}
if ( ModeCompatibilityMap.count( modes[i] ) > 0 ) {
std::vector<std::string> badModes = ModeCompatibilityMap[modes[i]];
for ( size_t j = 0; j < badModes.size(); ++j ) {
for ( size_t k = 0; k < modes.size(); ++k ) {
if ( badModes[j] == modes[k] ) {
return false;
}
}
}
}
} else if ( !IsMiscGameMode( modes[i] ) ) {
return false;
}
}
// 5. Assign the output variable with the game mode and return true
out = in;
return true;
}
std::string ExtractGameMode( const std::string &gameName ) {
std::vector<std::string> tokens = Tokenize( gameName );
std::string gameMode;
for ( size_t i = 0; i < tokens.size(); ++i ) {
if ( IsValidGameMode( tokens[i], gameMode ) ) {
return gameMode;
}
}
return "";
}
int main( int argc, char *argv[] ) {
InitializeModeCompatibilityMap();
std::string gameName = "DotA v6.60 -RDEM USA/CA LC";
std::string gameMode = ExtractGameMode( gameName );
std::cout << "Name: " << gameName << std::endl;
std::cout << "Mode: " << gameMode << std::endl;
return 0;
}
输出:
Name: DotA v6.60 -RDEM USA/CA LC
Mode: rdem
如果有人想查看此代码并让我知道他们会更改什么,我们将不胜感激。
谢谢。
最佳答案
创建 bool 数组来复制您放入注释中的表格。除了用“true”或“false”代替“X”或空白(所以“true”表示模式组合有效,“false”表示无效)。
使用此表查找组合是否有效:
bool IsSecondaryValidWithPrimary(unsigned int primaryIndex, unsigned int secondaryIndex)
{
static bool secondaryValidWithPrimary[numPrimaryModes][numSecondaryModes] = {...}
if (primaryIndex < numPrimaryModes && secondaryIndex < numSecondaryModes)
{
return secondaryValidWithPrimary[primaryIndex][secondaryIndex]
}
else
{
//... this should never happen, throw your favorite exception
}
}
当然,这需要您将每个 2 个字符的字符串转换为要测试的正确数组索引。遍历所有可能的组合并检查其是否有效。我怀疑您是否真的关心此设置中的性能,所以这应该能很好地工作。
对其他有效性检查(次要与另一个次要)或您具有兼容性规则的任何其他模式组合执行相同的操作。
关于C++ - 如何提取字符串中的有效字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/986400/
如何使用 SPListCollection.Add(String, String, String, String, Int32, String, SPListTemplate.QuickLaunchO
我刚刚开始使用 C++ 并且对 C# 有一些经验,所以我有一些一般的编程经验。然而,似乎我马上就被击落了。我试过在谷歌上寻找,以免浪费任何人的时间,但没有结果。 int main(int argc,
这个问题已经有答案了: In Java 8 how do I transform a Map to another Map using a lambda? (8 个回答) Convert a Map>
我正在使用 node + typescript 和集成的 swagger 进行 API 调用。我 Swagger 提出以下要求 http://localhost:3033/employees/sear
我是 C++ 容器模板的新手。我收集了一些记录。每条记录都有一个唯一的名称,以及一个字段/值对列表。将按名称访问记录。字段/值对的顺序很重要。因此我设计如下: typedef string
我需要这两种方法,但j2me没有,我找到了一个replaceall();但这是 replaceall(string,string,string); 第二个方法是SringBuffer但在j2me中它没
If string is an alias of String in the .net framework为什么会发生这种情况,我应该如何解释它: type JustAString = string
我有两个列表(或字符串):一个大,另一个小。 我想检查较大的(A)是否包含小的(B)。 我的期望如下: 案例 1. B 是 A 的子集 A = [1,2,3] B = [1,2] contains(A
我有一个似乎无法解决的小问题。 这里...我有一个像这样创建的输入... var input = $(''); 如果我这样做......一切都很好 $(this).append(input); 如果我
我有以下代码片段 string[] lines = objects.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.No
这可能真的很简单,但我已经坚持了一段时间了。 我正在尝试输出一个字符串,然后输出一个带有两位小数的 double ,后跟另一个字符串,这是我的代码。 System.out.printf("成本:%.2
以下是 Cloud Firestore 列表查询中的示例之一 citiesRef.where("state", ">=", "CA").where("state", "= 字符串,我们在Stack O
我正在尝试检查一个字符串是否包含在另一个字符串中。后面的代码非常简单。我怎样才能在 jquery 中做到这一点? function deleteRow(locName, locID) { if
这个问题在这里已经有了答案: How to implement big int in C++ (14 个答案) 关闭 9 年前。 我有 2 个字符串,都只包含数字。这些数字大于 uint64_t 的
我有一个带有自定义转换器的 Dozer 映射: com.xyz.Customer com.xyz.CustomerDAO customerName
这个问题在这里已经有了答案: How do I compare strings in Java? (23 个回答) 关闭 6 年前。 我想了解字符串池的工作原理以及一个字符串等于另一个字符串的规则是
我已阅读 this问题和其他一些问题。但它们与我的问题有些无关 对于 UILabel 如果你不指定 ? 或 ! 你会得到这样的错误: @IBOutlet property has non-option
这两种方法中哪一种在理论上更快,为什么? (指向字符串的指针必须是常量。) destination[count] 和 *destination++ 之间的确切区别是什么? destination[co
This question already has answers here: Closed 11 years ago. Possible Duplicates: Is String.Format a
我有一个Stream一个文件的,现在我想将相同的单词组合成 Map这很重要,这个词在 Stream 中出现的频率. 我知道我必须使用 collect(Collectors.groupingBy(..)
我是一名优秀的程序员,十分优秀!