给定一个长度为 L 的大写字母的非空字符串S,任务是从给定的字符串找到最长的字符串,其中的字符按其 ASCII 码的降序和等差数列排列,使得公共差异应低至可能并且字符串的字符具有更高的 ASCII 值。
注意:该字符串至少包含三个不同的字符。
例子:
Input : S = “ABCPQR”
Output : “RQP”
Two strings of maximum length are possible – “CBA” and “RPQ”. But since
the string should be of higher ASCII value hence, the output is “RPQ”.
Input : S = “ADGJPRT”
Output : “JGDA”
方法:等差数列中最少 3 个字符的最大可能公差为 12。因此,使用哈希图预先计算字符串中存在的所有字符,然后从具有最大 ASCII 值的字符(即“Z”)迭代到该字符具有最小 ASCII 值,即“A”。如果当前字符存在于给定字符串,则将其视为等差数列序列的起始字符,并再次迭代所有可能的共同差异,即从 1 到 12。检查每个当前共同差异,如果该字符存在于给定字符串, 增加所需最长字符串的当前长度。现在,有两种情况需要更新最大长度和最小公差。
- 当前长度大于最大长度时。
- 当当前长度等于最大长度且当前公差小于最小公差时,则需要更新公差。
此外,每次更新这两个参数时,也必须更新字符串或等差数列序列的起始字符。
下面是上述方法的实现:
C++
// C++ Program to find the longest string
// with characters arranged in non-decreasing
// order of ASCII and in arithmetic progression
#include
using namespace std;
// Function to find the longest String
string findLongestString(string S)
{
// Stores the maximum length of required string
int maxLen = 0;
// Stores the optimal starting character of
// required string or arithmetic progression sequence
int bestStartChar;
// Stores the optimal i.e. minimum common difference
// of required string
int minCommonDifference = INT_MAX;
unordered_map mp;
for (int i = 0; i < S.size(); i++)
mp[S[i]] = true;
// Iterate over the loop in non decreasing order
for (int startChar = 'Z'; startChar > 'A'; startChar--) {
// Process further only if current character
// exists in the given string
if (mp[startChar]) {
// Iterate over all possible common differences
// of AP sequence and update maxLen accordingly
for (int currDiff = 1; currDiff <= 12; currDiff++) {
int currLen = 1;
// Iterate over the characters at any interval
// of current common difference
for (int ch = startChar - currDiff; ch >= 'A';
ch -= currDiff) {
if (mp[ch])
currLen++;
else
break;
}
// Update maxLen and other parameters if the currLen
// is greater than maxLen or if the current
// difference is smaller than minCommonDifference
if (currLen > maxLen || (currLen == maxLen
&& currDiff < minCommonDifference)) {
minCommonDifference = currDiff;
maxLen = currLen;
bestStartChar = startChar;
}
}
}
}
string longestString = "";
// Store the string in decreasing order of
// arithmetic progression
for (int i = bestStartChar;
i >= (bestStartChar - (maxLen - 1) * minCommonDifference);
i -= minCommonDifference)
longestString += char(i);
return longestString;
}
// Driver Code
int main()
{
string S = "ADGJPRT";
cout << findLongestString(S) << endl;
return 0;
}
Java
// Java Program to find the longest string
// with characters arranged in non-decreasing
// order of ASCII and in arithmetic progression
import java.util.*;
import java.lang.*;
public class GFG {
// Function to find the longest String
static String findLongestString(String S)
{
// Stores the maximum length of required string
int maxLen = 0;
// Stores the optimal starting character of
// required string or arithmetic progression sequence
int bestStartChar = 0;
// Stores the optimal i.e. minimum common difference
// of required string
int minCommonDifference = Integer.MAX_VALUE;
HashMap hm = new HashMap
();
for (int i = 0; i < S.length(); i++)
hm.put(S.charAt(i), true);
// Iterate over the loop in non decreasing order
for (int startChar = 'Z'; startChar > 'A'; startChar--) {
// Process further only if current character
// exists in the given string
if (hm.containsKey((char)startChar)) {
// Iterate over all possible common differences
// of AP sequence and update maxLen accordingly
for (int currDiff = 1; currDiff <= 12; currDiff++) {
int currLen = 1;
// Iterate over the characters at any interval
// of current common difference
for (int ch = startChar - currDiff; ch >= 'A';
ch -= currDiff) {
if (hm.containsKey((char)ch))
currLen++;
else
break;
}
// Update maxLen and other parameters if the currLen
// is greater than maxLen or if the current
// difference is smaller than minCommonDifference
if (currLen > maxLen || (currLen == maxLen
&& currDiff < minCommonDifference)) {
minCommonDifference = currDiff;
maxLen = currLen;
bestStartChar = startChar;
}
}
}
}
String longestString = "";
// Store the string in decreasing order of
// arithmetic progression
char ch;
for (int i = bestStartChar;
i >= (bestStartChar - (maxLen - 1) * minCommonDifference);
i -= minCommonDifference)
{
ch = (char)i;
longestString += ch;
}
return longestString;
}
// Driver Code
public static void main(String args[])
{
String S = "ADGJPRT";
System.out.println(findLongestString(S));
}
}
// This code is contributed by Nishant Tanwar
C#
// C# Program to find the longest string
// with characters arranged in non-decreasing
// order of ASCII and in arithmetic progression
using System;
using System.Collections ;
public class GFG {
// Function to find the longest String
static String findLongestString(String S)
{
// Stores the maximum length of required string
int maxLen = 0;
// Stores the optimal starting character of
// required string or arithmetic progression sequence
int bestStartChar = 0;
// Stores the optimal i.e. minimum common difference
// of required string
int minCommonDifference = Int32.MaxValue ;
Hashtable hm = new Hashtable ();
for (int i = 0; i < S.Length; i++)
hm.Add(S[i], true);
// Iterate over the loop in non decreasing order
for (int startChar = 'Z'; startChar > 'A'; startChar--) {
// Process further only if current character
// exists in the given string
if (hm.ContainsKey((char)startChar)) {
// Iterate over all possible common differences
// of AP sequence and update maxLen accordingly
for (int currDiff = 1; currDiff <= 12; currDiff++) {
int currLen = 1;
// Iterate over the characters at any interval
// of current common difference
for (int ch = startChar - currDiff; ch >= 'A';
ch -= currDiff) {
if (hm.ContainsKey((char)ch))
currLen++;
else
break;
}
// Update maxLen and other parameters if the currLen
// is greater than maxLen or if the current
// difference is smaller than minCommonDifference
if (currLen > maxLen || (currLen == maxLen
&& currDiff < minCommonDifference)) {
minCommonDifference = currDiff;
maxLen = currLen;
bestStartChar = startChar;
}
}
}
}
String longestString = "";
// Store the string in decreasing order of
// arithmetic progression
char ch1;
for (int i = bestStartChar;
i >= (bestStartChar - (maxLen - 1) * minCommonDifference);
i -= minCommonDifference)
{
ch1 = (char)i;
longestString += ch1;
}
return longestString;
}
// Driver Code
public static void Main()
{
String S = "ADGJPRT";
Console.WriteLine(findLongestString(S));
}
// This code is contributed by Ryuga
}
JGDA
时间复杂度:O(|S| + 26*12*26),其中|S|是字符串的大小。
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。