Advertisement
ElenaR1

MAP SDP

Oct 10th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. //!!!!!!!!!! http://comproguide.blogspot.bg/search?q=linked+list+c%2B%2B RESHENI ZADACHI za linked list
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string.h>
  5. #include <string>
  6. #include <assert.h>
  7. #include <map>
  8. #include <unordered_map>
  9.  
  10.  
  11. using namespace std;
  12.  
  13. bool func(int*a, int*b, int n1, int n2)
  14. {
  15.     map<int, bool> f;
  16.     for (size_t i = 0; i < n1; i++)
  17.     {
  18.         f[a[i]] = true;
  19.     }
  20.     map<int, bool>::iterator it = f.find(2);//Searches the container for an element with a key equivalent to k and
  21.     //returns an iterator to it if found, otherwise it returns an iterator to map::end.
  22.     cout<<"elem with key value 2" << it->second<<endl;//vrushta 1 t.k prosto sme dali da e true
  23.     for (size_t i = 0; i < n2; i++)
  24.     {
  25.         if (f.find(b[i]) != f.end())//ako element ot masiva b se sreshta v map-a vrushta false
  26.         {
  27.             return false;
  28.         }
  29.         return true;
  30.     }
  31.  
  32. }
  33. int max(int a, int b)
  34. {
  35.     if (a > b) return a;
  36.     else
  37.         return b;
  38. }
  39. int maxDistance(int*arr, int n)//namira nai-golqmoto razstoqnie mejdu 2 povtarshti vse elementa v masiva
  40. {
  41.     map<int, int> mp;
  42.     int max_dist = 0;
  43.     for (int i = 0; i<n; i++)
  44.     {
  45.         // If this is first occurrence of element, insert its
  46.         // index in map
  47.         if (mp.find(arr[i]) == mp.end())//find ne namira elementa and it returns an iterator to map::end -> elementa se sreshta za pruv put
  48.             mp[arr[i]] = i;
  49.  
  50.         // Else update max distance
  51.         else
  52.             max_dist = max(max_dist, i - mp[arr[i]]);
  53.     }
  54.  
  55.     return max_dist;
  56. }
  57. // C++ program to check if a string can be converted to
  58. // a string that has repeated substrings of length k.
  59.  
  60. // Returns true if str can be coverted to a string
  61. // with k repeated substrings after replacing k
  62. // characters.
  63. bool checkString(string str, long k)
  64. {
  65.     // Length of string must be a multiple of k
  66.     int n = str.length();
  67.     if (n%k != 0)
  68.         return false;
  69.  
  70.     // Map to store strings of length k and their counts
  71.     unordered_map<string, int> mp;
  72.     for (int i = 0; i<n; i += k)
  73.         mp[str.substr(i, k)]++;
  74.  
  75.     // If string is already a repition of k substrings,
  76.     // return true.
  77.     if (mp.size() == 1)
  78.         return true;
  79.  
  80.     // If number of distinct substrings is not 2, then
  81.     // not possible to replace a string.
  82.     if (mp.size() != 2)
  83.         return false;
  84.  
  85.     // One of the two distinct must appear exactly once.
  86.     // Either the first entry appears once, or it appears
  87.     // n/k-1 times to make other substring appear once.
  88.     if ((mp.begin()->second == (n / k - 1)) ||
  89.         mp.begin()->second == 1)
  90.         return true;
  91.  
  92.     return false;
  93. }
  94.  
  95. int main()
  96. {
  97.     int a[4] = { 1,2,5,7 };
  98.     int b[5] = { 10,9,0,4,6 };
  99.     cout << func(a, b, 4, 5) << endl;
  100.     map<int, char> mapp;
  101.  
  102.     int arr[] = { 3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 4, 2 };
  103.     int n = 12;
  104.     cout<<"max distance: " << maxDistance(arr, n) << endl;
  105.  
  106.  
  107.     mapp[1] = 'a';
  108.     mapp[2] = 'b';
  109.     mapp[3] = 'c';
  110.     cout << mapp[2] << endl;
  111.     mapp.insert(pair<int, char>(4, 'd'));
  112.     map<int, char>::iterator it;
  113.     for (it = mapp.begin(); it != mapp.end(); ++it)
  114.     {
  115.         cout << it->first << " " << it->second << endl;
  116.     }
  117.     it = mapp.find(3);
  118.     cout << "value found: " << it->second << endl;
  119.     mapp.erase(it);
  120.     //mapp.erase(3);
  121.     for (it = mapp.begin(); it != mapp.end(); ++it)
  122.     {
  123.         cout << it->first << " " << it->second << endl;
  124.     }
  125.  
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement