There are two arrays of characters, strings in simple terms, you need to add them up without duplicate elements, i.e. if given: "112пруу" and "341афж" , then it should issue "12пру34афж" , the solution seems to be on the surface, but there is no way (

  • Is order important? 1234ажпруф not good? - Harry
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky
  • What did not suit you the answers that you were given earlier on the same question? - Harry

6 answers 6

For example:

 string add(const string& a, const string& b) { bool f[256] = { false }; string res; for(unsigned char x: a) { if (f[x] == false) { res += x; f[x] = true; } } for(unsigned char x: b) { if (f[x] == false) { res += x; f[x] = true; } } return res; } 
  • For utf-8 so compact will no longer work :-( - avp
  • @avp This is for sure :) Then, in order to save O(n) , you will have to use unordered_set . - Harry

The solution is stupid in the forehead: we add characters from the source lines one by one, first checking whether it is already in the output line:

 std::wstring concat_unique(std::wstring in1, std::wstring in2) { std::wstring out = L""; for (auto i: in1 + in2) { if (out.find(i) == std::wstring::npos) { out += i; } } return out; } 
  • The only negative is formally O (n ^ 2). - Harry

For example:

int main(){ string s1 = "11245abc"; string s2 = "298da"; string res = ""; unordered_set<int> used; for (auto s : s1 + s2){ if (used.insert(s).second) res+=s; } cout << res<<endl; return 0; }

     string add(const string& a, const string& b) { bool f[256] = { false }; string res; for(unsigned char x: a) { if (f[x] == false) { res += x; f[x] = true; } } for(unsigned char x: b) { if (f[x] == false) { res += x; f[x] = true; } } return res; } 

      As a student question, I will not give a ready-made solution. I see that all stl are used, we will also use stl.

      The keyword is "no repetitive." Use std :: set. Read http://en.cppreference.com/w/cpp/container/set You must first go through the first line, adding characters to the set (read doc, looking for which function adds), then the second. set automatically contains only unique characters and in ascending order. The second step is to extract the symbols from the set. Here you need to use iterators.

       for (set<char>::iterator it = s.begin(); it != s.end(); ++it) { cout << ... } 

      In place of the dot, put the iterator dereference (as a pointer).
      All code should fit you in five lines.

        Hello! To add two strings, use strcat(char*,char*) as the first argument, place the first array. If you want, that there would not be identical symbols in the source array like this:

         char* sum(char* m1, char* m2) { int size; size = strlen(m1) + strlen(m2); //получаем общее кол-во символов char* m3 = new char[size]; //создаём динамический массив for (int i(0), ii(0), ic(0); i <= size; ic++, i++) { if (i < strlen(m1)) //для первого { int s = 0; for (int iii(0); iii <= i; iii++) if (m3[iii] == m1[i]) //если совпал { s = 1; ic--; break; } if (!s) { m3[ic] = m1[i]; } } else//для второго { int s = 0; for (int iii(0); iii <= i; iii++) if (m3[iii] == m2[ii]) //если совпал { s = 1; ic--; break; } if (!s) { m3[ic] = m2[ii]; } ii++; } } return m3; } int main() { char m1[10] = "asdzxcs"; char m2[10] = "dfjhsfsas"; cout << sum(m1, m2); getch(); return 0; } 

        Here is a good site http://cppstudio.com/ , he himself began to learn

        • Did you read the question carefully? - pavel
        • @pavel, oh I thought it was a typo, I'll rewrite it now, thanks) - Arthur Klochko
        • Return char *, work with it, although in the C ++ question, there is also a memory leak - int3
        • @ int3 well, yes, it was necessary to enter one more array and then push the string from the dynamic one, and then add the dynamic one. An example is not very, I agree - Arthur Klochko