计算数组中唯一字符串的数量
Counting number of unique strings in array
我一直在尝试编写一个简单的程序来计算数组中唯一字符串的数量,但我仍然想不出一个解决方案来解决如果字符串重复超过两次我该怎么办。
这是一个代码示例,其中 "Tommy" 在数组中存在 3 次。所以当我统计不唯一的个数时,应该是3个,只有2个名字是唯一的。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string stringArray[5] = { "Tommy", "Sammy", "Tommy", "Wally", "Tommy" }, repeatedArray[5];
int instance = 0, notUnique = 0, repeated = 0;
for (int i = 0; i < 5; i++)
{
for (int j = i + 1; j < 5; j++)
{
if (stringArray[i] == stringArray[j] && stringArray[i] != repeatedArray[repeated])
{
instance++;
}
}
if (instance == 1)
{
notUnique += 2;
}
else if (instance >= 2)
{
notUnique += instance + 1;
repeatedArray[repeated] = stringArray[i];
repeated++;
}
instance = 0;
}
cout << "Number of non-unique strings in array is :" << notUnique << endl;
}
编辑:我必须读两遍才能理解你想要计算的内容。值得庆幸的是,我想到的方法适用于此,所以:
一个简单的方法是以易于访问的方式记住您找到字符串的频率。我在考虑 std::map
:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
string stringArray[5] = { "Tommy", "Sammy", "Tommy", "Wally", "Tommy" };
std::map<string, int> count_find;
int non_unique_count = 0, unique_count = 0;
for(auto &s : stringArray) {
// Note: This uses that std::map::operator[] zero-initializes when it is
// called for a new key, so crucially, we start with 0 (not uninitialized)
int count = ++count_find[s];
if(count == 1) { // when a string is found the first time
++unique_count; // tentatively mark it as unique
} else if(count == 2) { // when it is found the second time
--unique_count; // unmark as unique
non_unique_count += 2; // and chalk both found strings up as non-unique
} else { // after that,
++non_unique_count; // just chalk up as non-unique.
}
}
std::cout << unique_count << ", " << non_unique_count << std::endl;
}
或者,您可以在循环结束后从 unique_count
计算 non_unique_count
,如
for(auto &s : stringArray) {
int count = ++count_find[s];
if(count == 1) { // when a string is found the first time
++unique_count; // tentatively mark it as unique
} else if(count == 2) { // when it is found the second time
--unique_count; // unmark as unique
}
}
int non_unique_count = 5 - unique_count;
你想统计一个数组中unique/non-unique个字符串的个数,所以只需要知道一个字符串是否唯一即可。
#include <iostream> #include <string> using namespace std;
int main() {
string stringArray[5]= {
"Tommy", "Sammy", "Tommy", "Wally", "Tommy"
}
;
bool repeate[5];//mark whether the string is unique or not
for(size_t i=0;
i<5;
++i) repeate[i]=false;
for (int i=0;
i < 5;
i++) {
for (int j=i + 1;
j < 5;
j++) {
if (stringArray[i]==stringArray[j]) {
repeate[i]=true;
repeate[j]=true;
}
}
}
int notUnique=0;
for(int i=0;
i<5;
++i) if(repeate[i]) ++notUnique;
cout <<"Number of non-unique strings in array is :" << notUnique << endl;
}
class 程序
{
static void Main(string[] args)
{
String[] input = {"abcd", "acbd", "adcb", "cdba", "bcda", "badc","sstvz","zvsst","wxvsl"};
foreach (var item in input)
{
Console.WriteLine(item);
}
Console.WriteLine(checkMatchedCombinationOfStrings(input));
Console.ReadKey();
}
static string sortString(string str)
{
string sortedString = string.Empty;
int[] indexArray = new int[str.Length];
for (int i = 0; i < str.Length; i++)
{
indexArray[i] = str[i];
if (i > 0)
{
for (int j = 0; j <= i; j++)
{
if (indexArray[j] > indexArray[i])
{
indexArray[i] = indexArray[i] + indexArray[j];
indexArray[j] = indexArray[i] - indexArray[j];
indexArray[i] = indexArray[i] - indexArray[j];
}
}
}
}
for (int i = 0; i < indexArray.Length; i++)
{
sortedString += ((char)indexArray[i]).ToString();
}
return sortedString;
}
static int checkMatchedCombinationOfStrings(String[] input)
{
HashSet<String> set = new HashSet<String>();
for (int i = 0; i < input.Length; i++)
{
var charArraySet = sortString(input[i]);
if (!set.Contains(charArraySet))
{
set.Add(charArraySet);
}
}
return set.Count;
}
}
我一直在尝试编写一个简单的程序来计算数组中唯一字符串的数量,但我仍然想不出一个解决方案来解决如果字符串重复超过两次我该怎么办。
这是一个代码示例,其中 "Tommy" 在数组中存在 3 次。所以当我统计不唯一的个数时,应该是3个,只有2个名字是唯一的。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string stringArray[5] = { "Tommy", "Sammy", "Tommy", "Wally", "Tommy" }, repeatedArray[5];
int instance = 0, notUnique = 0, repeated = 0;
for (int i = 0; i < 5; i++)
{
for (int j = i + 1; j < 5; j++)
{
if (stringArray[i] == stringArray[j] && stringArray[i] != repeatedArray[repeated])
{
instance++;
}
}
if (instance == 1)
{
notUnique += 2;
}
else if (instance >= 2)
{
notUnique += instance + 1;
repeatedArray[repeated] = stringArray[i];
repeated++;
}
instance = 0;
}
cout << "Number of non-unique strings in array is :" << notUnique << endl;
}
编辑:我必须读两遍才能理解你想要计算的内容。值得庆幸的是,我想到的方法适用于此,所以:
一个简单的方法是以易于访问的方式记住您找到字符串的频率。我在考虑 std::map
:
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
string stringArray[5] = { "Tommy", "Sammy", "Tommy", "Wally", "Tommy" };
std::map<string, int> count_find;
int non_unique_count = 0, unique_count = 0;
for(auto &s : stringArray) {
// Note: This uses that std::map::operator[] zero-initializes when it is
// called for a new key, so crucially, we start with 0 (not uninitialized)
int count = ++count_find[s];
if(count == 1) { // when a string is found the first time
++unique_count; // tentatively mark it as unique
} else if(count == 2) { // when it is found the second time
--unique_count; // unmark as unique
non_unique_count += 2; // and chalk both found strings up as non-unique
} else { // after that,
++non_unique_count; // just chalk up as non-unique.
}
}
std::cout << unique_count << ", " << non_unique_count << std::endl;
}
或者,您可以在循环结束后从 unique_count
计算 non_unique_count
,如
for(auto &s : stringArray) {
int count = ++count_find[s];
if(count == 1) { // when a string is found the first time
++unique_count; // tentatively mark it as unique
} else if(count == 2) { // when it is found the second time
--unique_count; // unmark as unique
}
}
int non_unique_count = 5 - unique_count;
你想统计一个数组中unique/non-unique个字符串的个数,所以只需要知道一个字符串是否唯一即可。
#include <iostream> #include <string> using namespace std;
int main() {
string stringArray[5]= {
"Tommy", "Sammy", "Tommy", "Wally", "Tommy"
}
;
bool repeate[5];//mark whether the string is unique or not
for(size_t i=0;
i<5;
++i) repeate[i]=false;
for (int i=0;
i < 5;
i++) {
for (int j=i + 1;
j < 5;
j++) {
if (stringArray[i]==stringArray[j]) {
repeate[i]=true;
repeate[j]=true;
}
}
}
int notUnique=0;
for(int i=0;
i<5;
++i) if(repeate[i]) ++notUnique;
cout <<"Number of non-unique strings in array is :" << notUnique << endl;
}
class 程序 {
static void Main(string[] args)
{
String[] input = {"abcd", "acbd", "adcb", "cdba", "bcda", "badc","sstvz","zvsst","wxvsl"};
foreach (var item in input)
{
Console.WriteLine(item);
}
Console.WriteLine(checkMatchedCombinationOfStrings(input));
Console.ReadKey();
}
static string sortString(string str)
{
string sortedString = string.Empty;
int[] indexArray = new int[str.Length];
for (int i = 0; i < str.Length; i++)
{
indexArray[i] = str[i];
if (i > 0)
{
for (int j = 0; j <= i; j++)
{
if (indexArray[j] > indexArray[i])
{
indexArray[i] = indexArray[i] + indexArray[j];
indexArray[j] = indexArray[i] - indexArray[j];
indexArray[i] = indexArray[i] - indexArray[j];
}
}
}
}
for (int i = 0; i < indexArray.Length; i++)
{
sortedString += ((char)indexArray[i]).ToString();
}
return sortedString;
}
static int checkMatchedCombinationOfStrings(String[] input)
{
HashSet<String> set = new HashSet<String>();
for (int i = 0; i < input.Length; i++)
{
var charArraySet = sortString(input[i]);
if (!set.Contains(charArraySet))
{
set.Add(charArraySet);
}
}
return set.Count;
}
}