Generate a single tape Turing machine over the alphabet [a, b, c, x, y, z] such
ID: 3571365 • Letter: G
Question
Generate a single tape Turing machine over the alphabet [a, b, c, x, y, z] such that the input is received in three distinct sections:
Input of (a+x)* where the number of ‘a ‘ input characters is odd, every time two ‘a’ characters are found they are replaced with x’s.
Followed by b(a+b+x)* where the number of ‘b’ input characters is odd, every time two ‘b’ characters are found they are replaced with y’s, all a’s and all x’s are replaced with y
Followed by c(a+b+c+x+y)* where the number of ‘c’ input characters is odd, everytime two ‘c’ characters are found they are replaced with ‘z’ characters, all a’s, b’s, x’s and y’s are replaced with z’s.
Thus if the input string is: axxaxxaxxbaxbaxbcabxycabxycxy
Then the output string is: xxxxxxaxxyyyyyybzzzzzzzzzzczz
Explanation / Answer
import java.util.*;
class C
{
public static void main(String args[])
{
int m,l=0,counta=0,countb=0,countc=0;
Scanner sc=new Scanner(System.in);
StringBuffer str=new StringBuffer(); //taking input
str.append(sc.nextLine());
for(int i=0;i<str.length();i++) //finding index of 2 input and counting no of a
{
if(str.charAt(i)=='a')
counta++;
if(str.charAt(i)!='b')
{
l++;
}
else
{
break;
}
}
m=l;
for(int i=l;i<str.length();i++) //finding index of 3input and counting no of b
{
if(str.charAt(i)=='b')
countb++;
if(str.charAt(i)!='c')
{
m++;
}
else
{
break;
}
}
for(int i=m;i<str.length();i++) //counting no of c's
{
if(str.charAt(i)=='c')
countc++;
}
for(int i=0;i<=l-1;i++)
{
if(counta>1&&str.charAt(i)=='a')
{
counta--;
str.setCharAt(i,'x');
}
else if(str.charAt(i)=='a') //if last a is encounter we will not convert it
{
str.setCharAt(i,'a');
}
else
{
str.setCharAt(i,'x');
}
}
for(int i=l;i<=m-1;i++)
{
if(countb>1&&str.charAt(i)=='b')
{
countb--;
str.setCharAt(i,'y');
}
else if(str.charAt(i)=='b') //if last b is encounter we will not convert it
{
str.setCharAt(i,'b');
}
else
{
str.setCharAt(i,'y');
}
}
for(int i=m;i<str.length();i++)
{
if(countc>1&&str.charAt(i)=='c')
{
countc--;
str.setCharAt(i,'z');
}
else if(str.charAt(i)=='c') //if last c is encounter we will not convert it
{
str.setCharAt(i,'c');
}
else
{
str.setCharAt(i,'z');
}
}
System.out.print(str); //printing the output
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.