33,027
社区成员




#include <stdio.h>
#include <set>
#include <iostream>
#include <string>
// using dynamic programming
//
int n; // length of expression
std::string input;
int map[200][200]; // avoid dynamic memory allocation
// S(i,j) : 1 Set
// 2 Non set list
// -1 not yet determined
// 0 Not a set nor a list
inline int& S(int i, int j)
{
return map[i][j];
}
// j>=i guranteed
//
// calculate (if necessary) and return
// 0 if input i to j doesn't form a set nor non-set list
// 1 if input i to j forms a set
// 2 if input i to j forms a non-set list
//
int WhatIsThis(int i, int j)
{
if( S(i,j)==-1) // not filled yet
{
// try to see if it's a set first (note a set is also a list)
// a set must start with '{' end end with '}'
//
if(input[i]=='{' && input[j]=='}'){
if(j<=i+2) // 0 or 1 element set
S(i,j)=1;
else if(WhatIsThis(i+1,j-1)>0)
S(i,j)=1;
}
// if not a set, is it a list?
if( S(i,j)!=1 ){
if(i==j) // Atom is a list by definition
S(i,j)=2;
else{
// if exists k in (i,j) so that input[k]==',' and
// WhatIsThis(i,k-1)>0 &&WhatIsThis(k+1,j)>0, it's
// a non-set list
for(int k=i+1; k<j; ++k){
if(input[k]==',' &&
WhatIsThis(i,k-1)>0 &&
WhatIsThis(k+1,j)>0
){
S(i,j)=2;
break;
}
}
// Finally, neither a set nor a non-set List
if(S(i,j)==-1)
S(i,j)=0;
}
}
}
return S(i,j);
}
void solve(int i)
{
for(int j=0; j<n; ++j)
for(int k=j; k<n; ++k)
map[j][k]=-1;
WhatIsThis(0,n-1);
/* for debug
* print the map
*
for(int ii=0; ii<n; ++ii){
for(int j=0; j<n; ++j){
printf("%2d%c",S(ii,j),j==n-1?'\n':' ');
}
}
*/
printf("Word# %d: %sSet\n",i,WhatIsThis(0,n-1)==1?"":"No ");
}
int main()
{
int question_cnt;
int c;
scanf("%d\n", & question_cnt);
for(int i=0; i<question_cnt; ++i){
std::getline(std::cin,input);
n=input.size();
solve(i+1);
}
}