| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
第二题我的源程序In Reply To:baidu网上决赛我第一名~~ Posted by:xreborner at 2005-09-28 18:07:20 #include <iostream>
#include <cstdio>
#include <memory>
#include <cstring>
using namespace std;
struct rule
{
char* Name;
int* Vars;
int Len;
int Result;
int Unknown;
};
struct variable
{
char* Name;
int Len;
int* Involves;
bool Deded;
int DedBy;
};
int RuleN, VarN, FactN, DedN;
rule Rules[300000];
variable Vars[300000];
int Facts[300000];
int Deds[300000];
int Goal;
template<class type>
type* clone(const type* Sour, size_t Size)
{
type* P = (type*)malloc(Size * sizeof(type));
memcpy(P, Sour, Size * sizeof(type));
return P;
}
int new_var(const char* Name)
{
const int Size = 499979;
static int Hash[Size];
static bool Mark = false;
if (!Mark)
{
Mark = true;
memset(Hash, -1, sizeof Hash);
}
int I, Key, Pos, Delta;
Key = 0;
for (I = 0; Name[I] != 0; I++) Key = (Key * 256 + (unsigned char)Name[I]) % Size;
Pos = Key;
Delta = 0;
for (; Hash[Pos] >= 0; Pos = (Pos + (Delta = (Delta + Key) % Size) + 1) % Size)
if (strcmp(Vars[Hash[Pos]].Name, Name) == 0) return Hash[Pos];
Vars[VarN].Name = clone(Name, strlen(Name) + 1);
Hash[Pos] = VarN;
return VarN++;
}
bool name_char(char Ch)
{
return Ch > ' ' && Ch != '&' && Ch != '-' && Ch != '>';
}
bool read_name(char* Name)
{
static char Next = 0;
while (Next <= ' '&& Next != EOF) Next = getchar();
if (Next == EOF) return false;
if (!name_char(Next))
{
*Name++ = Next;
*Name = 0;
Next = getchar();
return true;
}
while (name_char(Next))
{
*Name++ = Next;
Next = getchar();
}
*Name = 0;
return true;
}
void input(const char* GoalName)
{
int I, J, K;
static char Name[1001];
static int Array[300000];
VarN = 0;
sscanf(GoalName, "%s", Name);
Goal = new_var(Name);
freopen("facts.txt", "r", stdin);
FactN = 0;
while (scanf("%s", Name) > 0) Facts[FactN++] = new_var(Name);
freopen("rules.txt", "r", stdin);
RuleN = 0;
while (read_name(Name))
{
Rules[RuleN].Name = clone(Name, strlen(Name) + 1);
I = 0;
for (;;)
{
read_name(Name);
if (Name[0] == '-') break;
if (Name[0] == '&') continue;
Array[I++] = new_var(Name);
}
Rules[RuleN].Len = I;
Rules[RuleN].Vars = clone(Array, I);
read_name(Name);
read_name(Name);
Rules[RuleN].Result = new_var(Name);
RuleN++;
}
for (I = 0; I < VarN; I++) Vars[I].Len = 0;
for (I = 0; I < RuleN; I++)
{
Rules[I].Unknown = Rules[I].Len;
for (J = 0; J < Rules[I].Len; J++) Vars[Rules[I].Vars[J]].Len++;
}
for (I = 0; I < VarN; I++)
{
Vars[I].Involves = new int[Vars[I].Len];
Vars[I].Len = 0;
}
for (I = 0; I < RuleN; I++)
for (J = 0; J < Rules[I].Len; J++)
{
K = Rules[I].Vars[J];
Vars[K].Involves[Vars[K].Len++] = I;
}
}
bool search_data(int Var, int DedBy)
{
int I, J;
if (Var == Goal)
{
Vars[Var].Deded = true;
Vars[Var].DedBy = DedBy;
return true;
}
if (Vars[Var].Deded) return false;
Vars[Var].Deded = true;
Vars[Var].DedBy = DedBy;
for (I = 0; I < Vars[Var].Len; I++)
{
J = Vars[Var].Involves[I];
Rules[J].Unknown--;
if (Rules[J].Unknown == 0 && !Vars[Rules[J].Result].Deded)
{
Deds[DedN++] = J;
if (search_data(Rules[J].Result, J)) return true;
}
}
return false;
}
void perform()
{
int I;
for (I = 0; I < VarN; I++) Vars[I].Deded = false, Vars[I].DedBy = -1;
DedN = 0;
for (I = 0; I < FactN; I++)
if (search_data(Facts[I], -1)) break;
}
void output_data(int Var)
{
int I, Rule;
if (Vars[Var].DedBy < 0) return;
Rule = Vars[Var].DedBy;
Vars[Var].DedBy = -1;
for (I = 0; I < Rules[Rule].Len; I++)
output_data(Rules[Rule].Vars[I]);
printf(" %s", Rules[Rule].Name);
}
void output_data()
{
if (Vars[Goal].Deded)
{
printf("TRUE data");
output_data(Goal);
printf("\n");
}
else
printf("UNCERTAIN\n");
}
void output_goal(int Var)
{
int I, Rule;
if (Vars[Var].DedBy < 0) return;
Rule = Vars[Var].DedBy;
printf(" %s", Rules[Rule].Name);
Vars[Var].DedBy = -1;
for (I = 0; I < Rules[Rule].Len; I++)
output_goal(Rules[Rule].Vars[I]);
}
void output_goal()
{
if (Vars[Goal].Deded)
{
printf("TRUE goal");
output_goal(Goal);
printf("\n");
}
else
printf("UNCERTAIN\n");
}
int main(int argc, char* argv[])
{
static char Name[1001];
if (argc < 3) return 0;
input(argv[2]);
perform();
if (argv[1][0] == 'd') output_data();
else if (argv[1][0] == 'g') output_goal();
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator