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

Transfer in MIPS char * strtoupper(char s[]) { char c; c = s[0]; /* empty string...
Transfer in MIPS char * strtoupper(char s[]) { char c; c = s[0]; /* empty string */ if (c == 0) return s; /* convert the first character to upper case*/ if (c >= ‘a’ && d <= ‘z’) { c -= 32; s[0] = c; } /* convert the remaining characters*/ strtoupper(s + 1); return s; }
Consider the language defined as such: L = { w$w’ : w is a string of...
Consider the language defined as such: L = { w$w’ : w is a string of numbers 0-9 and can be of length ≥ 0, and w’ is the reverse string of w} Write a recursive grammar that defines all strings in the language. Using the language above, write the Java code (using recursion) that will recognize if a string is part of the language or not.
C Programming Language Please Given a string S and keyword string K, encrypt S using keyword...
C Programming Language Please Given a string S and keyword string K, encrypt S using keyword Cipher algorithm. Note: The encryption only works on alphabets. Numbers and symbols such as 1 to 9, -, &, $ etc remain unencrypted. Input:     Zombie Here     secret     where: First line represents the unencrypted string S. Second line represents the keyword string K. Output:     ZLJEFT DTOT Explanation: We used "secret" keyword there. Plain Text: A B C D E F G H I J K...
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.
Question II: Consider the language L below. If L is a regular language, construct the corresponding...
Question II: Consider the language L below. If L is a regular language, construct the corresponding DFA. If not, prove that L is not a regular language. L= {0n10n | n ≥1} Consider the language M consisting of those strings of 0’s and 1’s that have an equal number of 0’s and 1’s (not in any particular order). Suppose we know M is not a regular language. Consider the language N consisting of those strings of 0’s and 1’s that...
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:...
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...
how to accept string from shared memory using c language ?
how to accept string from shared memory using c language ?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT