如何从数组 C++ 中获取唯一字符串
How to get unique strings from array c++
我知道我的问题对某些人来说可能很愚蠢,但我整天都在谷歌上搜索并尝试制定自己的解决方案,但我失败了..请帮忙..
我需要从一个简单的字符串数组中打印所有唯一字符串。
示例:
输入:"Hi""my""name""Hi""potato""text""name""Hi"
输出:"my""potato""text"
我只打印一次所有内容("Hi"、"my"、"name"、"potato"、"text"),但我需要忽略所有内容什么是数组中的 2x 和更多次。
我的算法是:
1. 冒泡排序
- 使用基本的 for 和 if
仅打印排序序列中的最后一个字符串
.. if(array[i]!=array[i+1]) //做点什么...
更新:我误解了这个问题现在它作为问题的输出
简单地说,你可以计算每个字符串的出现次数,并只打印出现的字符串。
时间复杂度:O(N^2)
这是代码
#include<iostream>
#include<set>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n; // number of strings you want in your array
cin >> n;
string t; // get t and push back it in the vector
vector <string> words; //we use vector to store as we will push back them
for(size_t i = 1;i <= n;i++)
{
cin >> t;
words.push_back(t);
}
for(int i = 0;i < words.size();i++)
{
int cnt = 0;
for(int j = 0;j < words.size() && cnt < 2;j++)
{
if(words[i] == words[j])
cnt++;
}
if(cnt == 1) //its unique..print it
cout << words[i] <<endl;
}
}
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> words{"Hi", "my", "name", "Hi", "potato", "text", "name", "Hi"};
std::sort(words.begin(), words.end());
for (auto curr = words.begin(); curr != words.end(); ) {
// If working w/ few duplicate words:
auto next = std::find_if(
curr + 1, words.end(), [&](const auto& s) { return s != *curr; }
);
/* If working w/ many duplicate words:
auto next = std::upper_bound(curr + 1, words.end(), *curr); */
if (std::distance(curr, next) == 1) {
std::cout << *curr++ << '\n';
} else {
curr = next;
}
}
}
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<std::string> v = {
"Hi", "my", "name", "Hi", "potato", "text", "name", "Hi",
};
sort(v.begin(), v.end());
for (auto a = v.begin(), b = a; a != v.end(); a = b) {
b = find_if(b, v.end(), [&](string s) {return *b != s;});
if (distance(a, b) == 1)
cout << *a << '\n';
}
}
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> words{"Hi", "my", "name", "Hi", "potato", "text", "name", "Hi"};
std::vector<std::string> out;
std::sort(words.begin(), words.end());
std::unique_copy(words.begin(), words.end(), std::back_inserter(out));
std::set_difference(words.begin(), words.end(), out.begin(), out.end(), std::ostream_iterator<std::string>(std::cout, " "));
}
注意:未经过测试,因为我正在 phone 和床上写这篇文章。
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n],i,j,k;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=1;j<n;j++)
{
if(a[i]==a[j]&&i!=j)
{
a[i]=0;
}
}
}
for(i=0;i<n;i++)
{
if(a[i]!=0)cout<<a[i]<<" ";
}
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
int i,j;
string ss;
cin>>ss;
for(i=0;i<ss.length();i++)
{
for(j=1;j<ss.length();j++)
{
if(ss[i]==ss[j]&&i!=j)
{
ss[i]=' ';
}
}
}
for(i=0;i<ss.length();i++)
{
if(ss[i]!=' ')cout<<ss[i]<<" "; /// for unique element print
}
return 0;
}
我知道我的问题对某些人来说可能很愚蠢,但我整天都在谷歌上搜索并尝试制定自己的解决方案,但我失败了..请帮忙..
我需要从一个简单的字符串数组中打印所有唯一字符串。
示例:
输入:"Hi""my""name""Hi""potato""text""name""Hi"
输出:"my""potato""text"
我只打印一次所有内容("Hi"、"my"、"name"、"potato"、"text"),但我需要忽略所有内容什么是数组中的 2x 和更多次。
我的算法是: 1. 冒泡排序
- 使用基本的 for 和 if 仅打印排序序列中的最后一个字符串
.. if(array[i]!=array[i+1]) //做点什么...
更新:我误解了这个问题现在它作为问题的输出 简单地说,你可以计算每个字符串的出现次数,并只打印出现的字符串。 时间复杂度:O(N^2) 这是代码
#include<iostream>
#include<set>
#include <string>
#include <vector>
using namespace std;
int main()
{
int n; // number of strings you want in your array
cin >> n;
string t; // get t and push back it in the vector
vector <string> words; //we use vector to store as we will push back them
for(size_t i = 1;i <= n;i++)
{
cin >> t;
words.push_back(t);
}
for(int i = 0;i < words.size();i++)
{
int cnt = 0;
for(int j = 0;j < words.size() && cnt < 2;j++)
{
if(words[i] == words[j])
cnt++;
}
if(cnt == 1) //its unique..print it
cout << words[i] <<endl;
}
}
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> words{"Hi", "my", "name", "Hi", "potato", "text", "name", "Hi"};
std::sort(words.begin(), words.end());
for (auto curr = words.begin(); curr != words.end(); ) {
// If working w/ few duplicate words:
auto next = std::find_if(
curr + 1, words.end(), [&](const auto& s) { return s != *curr; }
);
/* If working w/ many duplicate words:
auto next = std::upper_bound(curr + 1, words.end(), *curr); */
if (std::distance(curr, next) == 1) {
std::cout << *curr++ << '\n';
} else {
curr = next;
}
}
}
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<std::string> v = {
"Hi", "my", "name", "Hi", "potato", "text", "name", "Hi",
};
sort(v.begin(), v.end());
for (auto a = v.begin(), b = a; a != v.end(); a = b) {
b = find_if(b, v.end(), [&](string s) {return *b != s;});
if (distance(a, b) == 1)
cout << *a << '\n';
}
}
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> words{"Hi", "my", "name", "Hi", "potato", "text", "name", "Hi"};
std::vector<std::string> out;
std::sort(words.begin(), words.end());
std::unique_copy(words.begin(), words.end(), std::back_inserter(out));
std::set_difference(words.begin(), words.end(), out.begin(), out.end(), std::ostream_iterator<std::string>(std::cout, " "));
}
注意:未经过测试,因为我正在 phone 和床上写这篇文章。
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
int a[n],i,j,k;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
for(j=1;j<n;j++)
{
if(a[i]==a[j]&&i!=j)
{
a[i]=0;
}
}
}
for(i=0;i<n;i++)
{
if(a[i]!=0)cout<<a[i]<<" ";
}
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
int i,j;
string ss;
cin>>ss;
for(i=0;i<ss.length();i++)
{
for(j=1;j<ss.length();j++)
{
if(ss[i]==ss[j]&&i!=j)
{
ss[i]=' ';
}
}
}
for(i=0;i<ss.length();i++)
{
if(ss[i]!=' ')cout<<ss[i]<<" "; /// for unique element print
}
return 0;
}