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.
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 ?
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a...
Implement two functions in C language: stringLength() - Takes a pointer to a string, and a pointer to an int variable. It computes the length of the string and updates the int variable with the length. stringCopy() - Copies one string onto another using pointers #include<stdio.h> #define MAX_STR_LEN 2048 void stringLength(char *str, int *len) { // This function computes the length of the string // at the address in the pointer *str. Once the length // has been determined, it...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX INPUT: B C D A OUTPUT: D C B A #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]) {         int MAX = 100000;         int i =0;         int k =0;         int j =0;         char array[MAX];         char split[] = " ,.-!?()0123456789";         int n = 0;         char second[MAX];         printf("Please enter in a String: ");...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT