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:

Recommended Answers

All 5 Replies

Member Avatar for iamthwee

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.

Member Avatar for iamthwee

A double queue will do I guess.

Yeah, with a recursive function perhaps:

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

using namespace std;

class Fools
{
public:
   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.pop_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 );
   cin.get();
}
commented: Are you featured poster because you give away codes -1
bool conjugated(const std::string& s1, const std::string& s2)
{
  return s1.size() == s2.size() &&
        (s1+ s1).find(s2) != std::string::npos;
}

;)

commented: You amaze me everytime, again and again, you write the best and most efficient code possible :) +4
commented: I concur +19
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.