Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- /* wrong code written by me
- int countParenth(char sym[],char op[],int n){
- int total[n][n],F[n][n];
- for(int i=0;i<n;i++)
- total[i][i]=sym[i]=='T'?1:0;
- for(int i=0;i<n;i++)
- F[i][i]=sym[i]=='F'?1:0;
- for(int gap=1;gap<n;gap++){
- for(int i=0,j=gap;j<n;i++,j++){
- int valT=0,valF=0;
- for(int k=i;k<j;k++){
- int first=total[i][k],sec=total[k+1][j];
- int res=0;
- if(op[k]=='|')
- res=first||sec;
- if(op[k]=='&')
- res=first&&sec;
- if(op[k]=='^')
- res=first^sec;
- if(res){
- int a=first==0?1:first;
- int b=sec==0?1:sec;
- valT+=a*b;
- }
- else{
- int a=first==0?1:first;
- int b=sec==0?1:sec;
- valF+=a*b;
- }
- }
- total[i][j]=total[j][i]=valT;
- F[i][j]=F[j][i]=valF;
- }
- }
- return total[0][n-1];
- }
- */
- int countParenth(char sym[],char ope[],int n){ //TT |
- int T[n][n],F[n][n];
- for(int i=0;i<n;i++){
- T[i][i]=sym[i]=='T'?1:0;
- F[i][i]=sym[i]=='F'?1:0;
- }
- for(int gap=1;gap<n;gap++){
- for(int i=0,j=gap;j<n;i++,j++){
- T[i][j]=0,F[i][j]=0;
- for(int k=i;k<j;k++){
- int tik=T[i][k]+F[i][k];
- int tkj=T[k+1][j]+F[k+1][j];
- if(ope[k]=='|'){
- F[i][j]+=F[i][k]*F[k+1][j];
- T[i][j]+=tik*tkj-F[i][k]*F[k+1][j];
- }
- if(ope[k]=='&'){
- T[i][j]+=T[i][k]*T[k+1][j];
- F[i][j]+=tik*tkj-T[i][k]*T[k+1][j];
- }
- if(ope[k]=='^'){
- T[i][j]+=F[i][k]*T[k+1][j]+T[i][k]*F[k+1][j];
- F[i][j]+=F[i][k]*F[k+1][j]+T[i][k]*T[k+1][j];
- }
- }
- }
- }
- return T[0][n-1];
- }
- int main(){
- char symbols[] = "TTFT";
- char operators[] = "|&^";
- int n = 4;
- cout << countParenth(symbols,operators,n);
- return 0;
- }
Add Comment
Please, Sign In to add comment