Advertisement
Guest User

Untitled

a guest
Jan 19th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. //task: given a string S, count how many times do you need to change letters so that every letter is different
  2. //code
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. int main()
  7. {
  8.     int start_s=clock();
  9.     std::string input;
  10.     ifstream file;
  11.     int random;
  12. /* read input from file        
  13.     file.open("KB.inp");       
  14.     file >> random >> input;
  15.     cout << "read file";
  16. */
  17. /* direct input
  18.     cin >> random >> input;
  19. */     
  20.     std::string alpha="abcdefghijklmnopqrstuvwxyz";
  21.     int answer=0;
  22.     int characterCount=0;
  23.     int temp=0;
  24.     cin >> input;
  25.     int size=input.size();
  26.     for (int i=0;i<26;i++)
  27.     {
  28.         temp=count(input.begin(), input.end(), alpha.at(i));
  29.         characterCount += temp;
  30.         if (temp>0){answer+=temp-1;}
  31.         if (characterCount==size){break;}
  32.     }
  33.     cout << answer << '\n';
  34.     int stop_s=clock();
  35.     cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC) << " seconds";
  36. }
  37.  
  38. //input
  39. [character count]
  40. [string]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement