-
Notifications
You must be signed in to change notification settings - Fork 0
/
FOOL-1750673-src.c
97 lines (87 loc) · 1.62 KB
/
FOOL-1750673-src.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <string.h>
char isAtom[200][200], isList[200][200], isSet[200][200];
int n;
char s[201];
char ifAtom(int, int);
char ifSet(int, int);
char ifList(int, int);
char ifAtom(int i, int j)
{
if(i<0 || j<0 || i>=n || j>=n)
return 0;
if(isAtom[i][j]!=-1)
return isAtom[i][j];
isAtom[i][j]=ifSet(i, j);
return isAtom[i][j];
}
char ifList(int i, int j)
{
int k;
if(i<0 || j<0 || i>=n || j>=n)
return 0;
if(isList[i][j]!=-1)
return isList[i][j];
isList[i][j]=ifAtom(i, j);
if(isList[i][j])
return 1;
for(k=i+1; k<j; k++)
if(s[k]==',' && ifAtom(i, k-1) && ifList(k+1, j))
{
isList[i][j]=1;
return 1;
}
isList[i][j]=0;
return 0;
}
char ifSet(int i, int j)
{
if(i<0 || j<0 || i>=n || j>=n)
return 0;
if(isSet[i][j]!=-1)
return isSet[i][j];
return (isSet[i][j]=ifList(i+1, j-1));
}
int main()
{
int t, i, j, test;
scanf("%d\n", &t);
for(test=1; test<=t; test++)
{
gets(s);
n=strlen(s);
for(i=0; i<n; i++)
for(j=i; j<n; j++)
isAtom[i][j]=isList[i][j]=isSet[i][j]=-1;
//Base Case for Atom
for(i=0; i<n; i++)
isAtom[i][i]=1;
//Base Case for Set
for(i=0; i<n; i++)
{
isSet[i][i]=0;
if(s[i]!='{')
for(j=i+1; j<n; j++)
isSet[i][j]=0;
if(s[i]!='}')
for(j=i-1; j>=0; j--)
isSet[j][i]=0;
}
for(i=0; i<n-1; i++)
if(s[i]=='{' && s[i+1]=='}')
isSet[i][i+1]=1;
else
isSet[i][i+1]=0;
//Base Case for List
for(i=0; i<n; i++)
isList[i][i]=1;
for(i=0; i<n-1; i++)
isList[i][i+1]=isSet[i][i+1];
i=ifSet(0, n-1);
if(i)
printf("Word #%d: Set\n", test);
else
printf("Word #%d: No Set\n", test);
}
return 0;
}