Help me solve this problem, dont worry, im just starting out so its all very basic...and easy to you guys, i would imagine.

Two strings X and Y are said to be conjugates if, by shifting the symbols of Y in a cyclic fashion zero or more times, you can arrive at X. For example, the strings ababba and abbaab are conjugates because by shifting the symbols of the latter two positions to the right ( or, equivalently, four positions to the left), you arrive at the former. On the other hand, ababba and abaabb are not conjugates, as you can easily verify. It is clear from the definition that two strings cannot be conjugates unless they have the same length.

You are to develop a program that prompts the user to enter two strings (the maximum length for a string is 20 characters) and that by reporting whether the two are conjugates. The program terminates when the user enters the empty string.

Sample Program Execution

Enter 1st string: ababba
Enter 2nd string: abbaab
The two strings are conjugates.

Enter 1st string: ababba
Enter 2nd string: abaabb
The two strings are NOT conjugates.

Enter 1st string:

9 Years
Discussion Span
Last Post by ArkM

This sounds like a circular array problem, a queue /deque would be good to use here maybe depending on your homework pre-requisites.


A double queue will do I guess.

Yeah, with a recursive function perhaps:

#include <iostream>
#include <deque>
#include <string>

using namespace std;

class Fools
   void bar ( string t , string u , int count )
      if ( count < t.length() )
         deque <string> test;
         int s = t.length();

         for ( int i = 0 ; i < s; i++ )
            test.push_back ( t.substr ( i, 1 ) );
         string tmp = test.back();

         test.push_front ( tmp );

         string build = "";
         for ( int i = 0 ; i < test.size(); i++ )
            build = build + test[i];

         if ( build == u )
            cout << "conjugate";
         bar ( build , u , count + 1 );

int main()
   Fools gold;

   gold.bar ( "ababba", "abbaab" , 0 );
Votes + Comments
Are you featured poster because you give away codes
bool conjugated(const std::string& s1, const std::string& s2)
  return s1.size() == s2.size() &&
        (s1+ s1).find(s2) != std::string::npos;


Votes + Comments
I concur
You amaze me everytime, again and again, you write the best and most efficient code possible :)
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.