UVa219 - Department of Redundancy Department(DFS)

Department of Redundancy Department 

When designing tables for a relational database, a functional dependency (FD) is used to express the relationship between the different fields. A functional dependency is concerned with the relationship of values of one set of fields to those of another set of fields.

The notation X->Y is used to denote that when supplied values to the field(s) in set X, the assigned value for each field in set Y can be determined. For example, if a database table is to contain fields for thesocial security number (S), name (N), address (A), and phone (P) and each person has been assigned a unique value for S, the S field functionally determines the NA and P fields. This is written as S->NAP.

Develop a program that will identify each redundant FD in each input group of FDs. An FD is redundant if it can be derived using other FDs in the group.

For example, if the group contains the FDs A->BB->C, and A->C, then the third FD is redundant since the field set C can be derived using the first two. (The A fields determine values for the B fields, which in turn determine values for the fields in C.) In the group A->BB->CC->AA->CC->B, and B->A, all the FDs are redundant.

Input

The input file contains an arbitrary number of groups of FDs. Each group is preceded by a line containing an integer no larger than 100 specifying the number of FDs in that group. A group with zero FDs indicates the end of the input.

Each FD in the group appears on a separate line containing two non-empty lists of field names separated by the characters - and >. The lists of field names contain only uppercase alphabetic characters. Functional dependency lines contain no blanks or tabs. There are no trivially redundant FDs (for example, A->A).

For identification purposes, groups are numbered sequentially, starting with 1; the FDs are also numbered sequentially, starting with 1 in each group.

Output

For each group, in order, your program must identify the group, each redundant FD in the group, and a sequence of the other FDs in the group which were used to determine the indicated FD is redundant. If more than one sequence of FDs can be used to show another FD is redundant, any such sequence is acceptable, even if it is not the shortest proof sequence. Each FD in an acceptable proof sequence must, however, be necessary.

If a group of FDs contains no redundancy, display No redundant FDs.

There should be a blank line after every test case

Sample Input

3
A->BD
BD->C
A->C
6
P->RST
VRT->SQP
PS->T
Q->TR
QS->P
SR->V
5
A->B
A->C
B->D
C->D
A->D
3
A->B
B->C
A->D
0

Sample Output

Set number 1
     FD 3 is redundant using FDs: 1 2

Set number 2
     FD 3 is redundant using FDs: 1
     FD 5 is redundant using FDs: 4 6 2

Set number 3
     FD 5 is redundant using FDs: 1 3
          --OR--
     FD 5 is redundant using FDs: 2 4

Set number 4
     No redundant FDs.
     
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class Main
{
	public static final boolean DEBUG = true;
	public static int N = 110;
	public BufferedReader cin;
	public PrintWriter cout;
	public StreamTokenizer tokenizer;
	public Node[] node;
	public int n, cur;
	public boolean[] vis;
	public int[] path;
	
	static class Node 
	{
		int a, b;
	}
	
	public void init()
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(new FileInputStream("e:\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}
			
			cout = new PrintWriter(new OutputStreamWriter(System.out));
			tokenizer = new StreamTokenizer(cin);
			tokenizer.wordChars('-', '-');
			tokenizer.wordChars('>', '>');
			node = new Node[N];
			for (int i = 0; i < N; i++) {
				node[i] = new Node();
			}
			
			vis = new boolean[N];
			path = new int[N];
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String next()
	{
		try {
			tokenizer.nextToken();
			if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null;
			else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) return String.valueOf((int)(tokenizer.nval));
			else if (tokenizer.ttype == StreamTokenizer.TT_WORD) return tokenizer.sval;
			else return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public void parse(String s, int n)
	{
		boolean flag = false;
		
		node[n].a = node[n].b = 0;
		
		for (int i = 0, len = s.length(); i < len; i++) {
			char ch = s.charAt(i);
			if (ch == '>') flag = true;
			if (Character.isUpperCase(ch)) {
				if (!flag) node[n].a |= 1 << (ch - 'A');
				node[n].b |= 1 << (ch - 'A');
			}
		}
	}
	
	public boolean input()
	{
		n = Integer.parseInt(next());
		if (n == 0) return false;
		
		for (int i = 0; i < n; i++) {
			String s = next();
			parse(s, i);
		}
		return true;
	}
	
	public boolean dfs(int len, int status)
	{
		path[len] = -1;
		
		if ((status & node[cur].b) == node[cur].b) return true;
		
		int now = status;
		
		while (true) {
			boolean flag = false;
			
			for (int i = 0; i < n; i++) {
				if (vis[i]) continue;
				if ((now & node[i].a) != node[i].a) continue;
				if ((now | node[i].b) == now) continue;
				
				now |= node[i].b;
				flag = true;
			}
			
			if (!flag || (now & node[cur].b) == node[cur].b) break;
		}
		
		if ((now & node[cur].b) != node[cur].b) return false;
		
		now = status;
		
		for (int i = 0; i < n; i++) {
			if (vis[i]) continue;
			if ((now & node[i].a) != node[i].a) continue;
			if ((now | node[i].b) == now) continue;
			
			vis[i] = true;
			if (dfs(len, now)) return true;
			
			path[len] = i;
			now |= node[i].b;
			if (dfs(len + 1, now)) return true;
			
			break;
		}
		
		return false;
	}
	
	public void solve(int cas)
	{
		int ansnum = 0;
		
		
		cout.printf("Set number %d", cas);
		cout.println();
		
		
		
		for (cur = 0; cur < n; cur++) {
			Arrays.fill(vis, false);
			vis[cur] = true;
			if (dfs(0, node[cur].a)) {
				ansnum++;
				cout.printf("     FD %d is redundant using FDs:", cur + 1);
				for (int i = 0; path[i] != -1; i++) {
					cout.print(" " + (path[i] + 1));
				}
				cout.println();
			}
		}
		
		if (ansnum == 0) {
			cout.println("     No redundant FDs.");
		}
		cout.println();
		cout.flush();
		
	}
	
	public static void main(String[] args)
	{
		Main solver = new Main();
		solver.init();
		
		int t = 1;
		while (solver.input()) {
			solver.solve(t++);
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值