Advertisement
Guest User

Untitled

a guest
Oct 15th, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. /*
  2. ID: bradyawn
  3. PROG: beads
  4. LANG: C++11
  5. */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. int mod(int x, int y)
  14. {
  15.     while (x < 0)
  16.     {
  17.         x += y;
  18.     }
  19.     return (x % y);
  20. }
  21.  
  22. int findLen(string &neck, int index, int N) //BOTHWARDS!!!111!!!
  23. {
  24.     int forNum;
  25.     int backNum;
  26.     char color = 'w';
  27.     int i = index;
  28.     //Count Forwards
  29.     for (forNum = 0; forNum < N; forNum++ )
  30.     {
  31.         i = mod(index+forNum, N);
  32.         if (color == 'w' && neck[i] != 'w')
  33.             color = neck[i];
  34.        
  35.         if((color != 'w')&&(color != neck[i])&&(neck[i] != 'w'))
  36.             break;
  37.     }
  38.    
  39.     //Backwards
  40.     color = 'w';
  41.     i = mod(index-1, N);
  42.     for (backNum = 0; backNum < N; backNum++)
  43.     {
  44.         i = mod(index-1-backNum, N);
  45.         if (color == 'w' && neck[i] != 'w')
  46.             color = neck[i];
  47.        
  48.         if((color != 'w')&&(color != neck[i])&&(neck[i] != 'w'))
  49.             break;
  50.     }
  51.    
  52.     int numBeads = forNum + backNum;
  53.     if (numBeads > N) numBeads = N;
  54.     return numBeads;
  55. }
  56.  
  57. int main ()
  58. {
  59.    
  60.     ifstream inf("beads.in");
  61.     ofstream outf("beads.out");
  62.     // Solution
  63.    
  64.     //Gathering Input
  65.     int N; //amount of beads
  66.     string neck; //necklace sequence
  67.     int max = 2;
  68.    
  69.     inf >> N;
  70.     inf >> neck;
  71.    
  72.    
  73.     for (int i = 0; i < N; i++) //brute force
  74.     {
  75.         int cur = findLen(neck, i, N);
  76.         if (cur > max) max = cur;
  77.     }
  78.    
  79.    
  80.     outf << max << endl;
  81.    
  82.     // End of Solution
  83.     inf.close();
  84.     outf.close();
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement