我知道我的问题对某人可能是愚蠢的,但是我整日用Google搜寻并尝试制定自己的解决方案,但我失败了..请帮助..
i know my question can be stupid for somebody, but i googled all day and try make my own solution but i failed.. Please help..
我需要从简单的字符串数组中打印所有uniqe字符串.
I need print all uniqe string from an simple array of strings.
示例:
输入:嗨"我的"名字"嗨"土豆"文本"名字"嗨"
input: "Hi" "my" "name" "Hi" "potato" "text" "name" "Hi"
输出:我的",土豆",文本"
output: "my" "potato" "text"
我使函数只打印一次所有内容("Hi","my","name","potato","text"),但是我需要忽略数组中2倍或更多次的所有内容.
I make just function to print everything once ("Hi", "my", "name", "potato", "text"), but i need ignore everything what is 2x and more times in array.
我的算法是: 1.按冒泡排序
My algorythm was: 1. sort by bubblesort
.. if(array [i]!= array [i + 1])//做点事情...
.. if(array[i]!=array[i+1]) //make something...
推荐答案update:我误解了这个问题,现在它可以作为问题的输出 您只需计算每个字符串的出现次数,并仅打印出现的字符串即可. 时间复杂度:O(N ^ 2) 这是代码
update : i was misunderstanding the question now it works as output in the question simply you can count the occurrences of every string and print only strings which occur ones.. time complexity : O(N^2) here is the code
#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; } }更多推荐
如何从数组C ++获取唯一字符串
发布评论