Question

In: Computer Science

In C++ Consider the language L = { s$s' : s is a possibly empty string...

In C++

Consider the language
L = { s$s' : s is a possibly empty string of characters other than $ , s' = reverse( s )}
as defi ned in Chapter 6 . Write a recognition algorithm for this language that uses both a queue and a stack. Thus,
as you traverse the input string, you insert each character of s into a queue and each character of s' into a stack.
Assume that each input string contains exactly one $ .

Solutions

Expert Solution

Summary :

Notes :
we implement the algorithm as following :
Iterate thru given string and perform following for each char

state = 0
length1 = 0
while input char is not 'S'
push the char to queue
   length1 += 1

if reached end of string :
return error

   length2 = 0 ;
while input char is not end of string
push the char to stack .
   length2 += 1

   if ( length1 != length2 )
       return error
  
   while ( !stack.empty() )
       if ( stack.top() != queue.front() )
           return error
       else
           stack.pop()
           queue.pop()
  

########################## Code #########################


#include <iostream>
#include <stdio.h>
#include <queue>
#include <stack>
#include <string>

using namespace std;

bool checkString( const string &str )
{
        stack<char> st ;
        queue<char> qu ;
        
        int len1 = 0 , len2 = 0 ;
        
        int state = 0 ;
        for( int i=0 ; i < str.size(); i++ )
        {
                if  ( str[i] == 'S' )
                {
                        if ( ( state == 0 )  )
                        state = 1 ;
                        else 
                        {
                                cout << "Multiple S found " << str[i] << " state " << state << " \n";
                                return false ;
                        }
                } else {
                        if ( state == 0 )
                        {
                                st.push(str[i]);
                                len1 += 1;
                        }
                        else 
                        {
                                qu.push(str[i]);
                                len2 +=1 ;
                        }
                }
        }
        if ( state == 0 )
        {
                cout << " No 'S' found \n";
                return false ;
        }
        
        if ( st.size() == qu.size() )
        {
                while ( !st.empty() )
                {
                        if ( st.top() != qu.front() )
                        {
                                cout << " s != s' ,  " << st.top() << " != " << qu.front() << "\n" ;
                                return false ;
                        } else 
                        {
                                st.pop();
                                qu.pop();
                        }
                }
                return true ;
        }
        else {
                cout << " --- length(s) != length(s') \n";
                return false ;
        }
        
}

int main()
{
        string s1 = "abcdefSdkjf";
        cout << " Checking for string - " << s1 << " ,  result - " << checkString(s1 ) << "\n";
        
        string s2 = "abcSdefSdkjf";
        cout << " Checking for string - " << s2 << " ,  result - " << checkString(s2 ) << "\n";
        
        string s3 = "abcdefSabcdef";    
        cout << " Checking for string - " << s3 << " ,  result - " << checkString(s3 ) << "\n";
        
        string s4 = "S";
        cout << " Checking for string - " << s4 << " ,  result - " << checkString(s4 ) << "\n";
        
        string s5 = "aS";
        cout << " Checking for string - " << s5 << " ,  result - " << checkString(s5 ) << "\n";
        
        string s6 = "Sn";
        cout << " Checking for string - " << s6 << " ,  result - " << checkString(s6 ) << "\n";
        
        string s7 = "abcdefSfedcba";
        cout << " Checking for string - " << s7 << " ,  result - " << checkString(s7 ) << "\n";
        
        string s8 = "abcdeffedcba";
        cout << " Checking for string - " << s8 << " ,  result - " << checkString(s8 ) << "\n";
        
}

#################### Output ###################


Related Solutions

Let L ⊆ Σ ∗ and define S(L), the suffix-language of L, as S(L) = {w...
Let L ⊆ Σ ∗ and define S(L), the suffix-language of L, as S(L) = {w ∈ Σ ∗ | x = yw for some x ∈ L, y ∈ Σ ∗ } Show that if L is regular, then S(L) is also regular.
Consider the following (unbalanced) chemical reaction: Fe2O3(s) + C(s) → Fe(l) + CO2(g)Fe2O3(s) + C(s) →...
Consider the following (unbalanced) chemical reaction: Fe2O3(s) + C(s) → Fe(l) + CO2(g)Fe2O3(s) + C(s) → Fe(l) + CO2(g) a How many grams of carbon are needed to react with 811.21 grams of Fe22O33? b How many grams of iron can be produced from 390.955 g of carbon? c you make 487.857 grams of CO22, how many grams of Fe are produced?
Let S be the set of natural numbers which can be written as a non-empty string...
Let S be the set of natural numbers which can be written as a non-empty string of ones followed by a non-empty string of zeroes. For example, 10, 111100 and 11100000 are all in S, but 11 and 1110011 are not in S. Prove that there exists a natural number n∈S, such that 2018 | n.
1. Let V be real vector space (possibly infinite-dimensional), S, T ∈ L(V ), and S...
1. Let V be real vector space (possibly infinite-dimensional), S, T ∈ L(V ), and S be in- vertible. Prove λ ∈ C is an eigenvalue of T if and only if λ is an eigenvalue of STS−1. Give a description of the set of eigenvectors of STS−1 associated to an eigenvalue λ in terms of the eigenvectors of T associated to λ. Show that there exist square matrices A, B that have the same eigenvalues, but aren’t similar. Hint:...
how to accept string from shared memory using c language ?
how to accept string from shared memory using c language ?
Consider an elastic string of length L whose ends are held fixed. The string is set...
Consider an elastic string of length L whose ends are held fixed. The string is set in motion with no initial velocity from an initial position u(x, 0) = f(x). Let L = 10 and a = 1 in parts (b) and (c). (A computer algebra system is recommended.) f(x) = 16x L ,      0 ≤ x ≤ L 4 , 4,      L 4 < x < 3L 4 , 16(L − x) L ,      3L 4...
Programming Language: C# Person Class Fields - password : string Properties + «C# property, setter private»...
Programming Language: C# Person Class Fields - password : string Properties + «C# property, setter private» IsAuthenticated : bool + «C# property, setter absent» SIN : string + «C# property, setter absent» Name : string Methods + «Constructor» Person(name : string, sin : string) + Login(password : string) : void + Logout() : void + ToString() : string Transaction Class Properties + «C# property, setter absent » AccountNumber : string + «C# property, setter absent» Amount : double + «C#...
Assume you already have a non-empty string S, which is guaranteed to contain only digits 0...
Assume you already have a non-empty string S, which is guaranteed to contain only digits 0 through 9. It may be of any length and any number of digits may occur multiple times. Starting from the front of the string, write a loop that jumps through the characters in the string according to the following rule: Examine the current character and jump that many characters forward in the string Stop if you jump past the end of the string, or...
L= {x^a y^b z^c | c=a+b(mod 2)} .Create a DFA and NFA for the language L....
L= {x^a y^b z^c | c=a+b(mod 2)} .Create a DFA and NFA for the language L. Solution and explanation please..
in C programming language char character [100] = "hello"; a string array variable It is given....
in C programming language char character [100] = "hello"; a string array variable It is given. By writing a function called TranslateString, By accessing the pointer address of this given string, returning the string's address (pointer address) by reversing the string Write the function and use it on the main function. Function void will not be written as. Return value pointer address it will be. Sweat operation on the same variable (character) It will be made. Declaration of the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT