我正在做一个问题,以获得获得给定字符串的最小字典值所需的最小移动。我首先添加了字符串并将其旋转以存储在地图上,并将其作为移动键。然后我返回了Map的第一个键,它给了我maxMoves来获得最小的词典字符串。例如: anadama => nadamaa => adamaan => damaana => amaanad => maanada => aanadam 。这里输出= 6,因为它需要六次旋转才能获得具有最小字典值的字符串。
当我查看提交时,它会显示Abort Called或Runtime Error- Message Masked。
这是我的代码:
int minRotate(string inscription) {
map<string, int> concatedString;
int inscriptionLength = inscription.length();
inscription += inscription;
int minRotation = 0, roatateNum = 1, temp = 1;
//Checking if the given is lexiographically small in number
if (inscription.empty() || inscription.find_first_not_of(inscription[0]) == string::npos)
return minRotation;
for (int i = 1; i <= inscriptionLength; i++) {
//rotate(inscription.begin(), inscription.begin()+1, inscription.end());
string tempString = inscription.substr(temp, inscriptionLength);
concatedString[tempString] = i;
temp++;
}
map<string, int>::iterator it = concatedString.begin();
minRotation = it->second;
return minRotation;
}