给定一个字符串数组(全部为小写字母),任务是以这样一种方式将它们分组,即一组中的所有字符串都是彼此的移位版本。如果两个字符串S 和 T 被称为移位,
S.length = T.length
and
S[i] = T[i] + K for
1 <= i <= S.length for a constant integer K
例如,字符串、{acd、dfg、wyz、yab、mop} 是彼此的移位版本。
Input : str[] = {"acd", "dfg", "wyz", "yab", "mop",
"bdfh", "a", "x", "moqs"};
Output : a x
acd dfg wyz yab mop
bdfh moqs
All shifted strings are grouped together.
我们可以看到一组的字符串中的模式,该字符串的所有的字符连续字符之间的差别是相等的。如上例所示,取 acd、dfg 和 mop
acd -> 2 1
dfg -> 2 1
拖把 -> 2 1
由于差异相同,我们可以使用它来识别属于同一组的字符串。这个想法是形成字符串差异作为关键。如果找到具有相同的差异字符串的字符串,那么这个字符串也属于同一组。例如,上面三个字符串有相同的差异字符串,即“21”。
在下面的实现中,我们将 ‘a’ 添加到每个差异并将 21 存储为“ba”。
C++
/* C/C++ program to print groups of shifted strings
together. */
#include
using namespace std;
const int ALPHA = 26; // Total lowercase letter
// Method to a difference string for a given string.
// If string is "adf" then difference string will be
// "cb" (first difference 3 then difference 2)
string getDiffString(string str)
{
string shift = "";
for (int i = 1; i < str.length(); i++)
{
int dif = str[i] - str[i-1];
if (dif < 0)
dif += ALPHA;
// Representing the difference as char
shift += (dif + 'a');
}
// This string will be 1 less length than str
return shift;
}
// Method for grouping shifted string
void groupShiftedString(string str[], int n)
{
// map for storing indices of string which are
// in same group
map< string, vector > groupMap;
for (int i = 0; i < n; i++)
{
string diffStr = getDiffString(str[i]);
groupMap[diffStr].push_back(i);
}
// iterating through map to print group
for (auto it=groupMap.begin(); it!=groupMap.end();
it++)
{
vector v = it->second;
for (int i = 0; i < v.size(); i++)
cout << str[v[i]] << " ";
cout << endl;
}
}
// Driver method to test above methods
int main()
{
string str[] = {"acd", "dfg", "wyz", "yab", "mop",
"bdfh", "a", "x", "moqs"
};
int n = sizeof(str)/sizeof(str[0]);
groupShiftedString(str, n);
return 0;
}
Python3
# Python3 program to print groups
# of shifted strings together.
# Total lowercase letter
ALPHA = 26
# Method to a difference string
# for a given string. If string
# is "adf" then difference string
# will be "cb" (first difference
# 3 then difference 2)
def getDiffString(str):
shift=""
for i in range(1, len(str)):
dif = (ord(str[i]) -
ord(str[i - 1]))
if(dif < 0):
dif += ALPHA
# Representing the difference
# as char
shift += chr(dif + ord('a'))
# This string will be 1 less
# length than str
return shift
# Method for grouping
# shifted string
def groupShiftedString(str,n):
# map for storing indices
# of string which are
# in same group
groupMap = {}
for i in range(n):
diffStr = getDiffString(str[i])
if diffStr not in groupMap:
groupMap[diffStr] = [i]
else:
groupMap[diffStr].append(i)
# Iterating through map
# to print group
for it in groupMap:
v = groupMap[it]
for i in range(len(v)):
print(str[v[i]], end = " ")
print()
# Driver code
str = ["acd", "dfg", "wyz",
"yab", "mop","bdfh",
"a", "x", "moqs"]
n = len(str)
groupShiftedString(str, n)
# This code is contributed by avanitrachhadiya2155
Javascript
输出:
a x
acd dfg wyz yab mop
bdfh moqs
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。