2016百度之星资格赛 E题

来源:互联网 发布:windows聚焦 win7 编辑:程序博客网 时间:2024/05/14 20:51

Problem E

Accepts: 170
Submissions: 804
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description

小度熊是一个尽职尽责的程序熊,每天产出数千行的代码,代码当中存在很多判断条件。度熊想让自己代码中的这些条件不存在交集。为了简化问题,一个条件可以是一个『简单条件』或者是一个『复合条件』,简单条件由『变量』、『比较符』和『运算数』组成,其中『变量』是用小写字符表示的一串字符,『运算数』仅为整数,『运算符』包括:<、>、<=、>=、==。分别代表:小于、大于、小于等于、大于等于和等于关系。简单条件的格式固定,左边为变量名,中间为操作符,右边为数字。若干个『简单条件』中间通过英文逗号组成一个『复合条件』,各『简单条件』之间是逻辑与的关系,例如:

简单条件: a > 100

复合条件: duxiong < 1000 , a > 100

Input

这里包括一组测试数据,第一行一个正整数 N(1≤N≤1000)N(1 \leq N \leq 1000)N(1N1000),接下来NNN 行,每行一个条件,条件可能是一个『简单条件』或者是一个『复合条件』。其中『变量』不会超过30个字符,『运算数』的绝对值在10,000以内。测试数据中,不同变量的数量不会超过30个。其中『变量』、『比较符』和『运算数』之前和之后都有可能出现若干空格字符。所有简单规则都是按照『变量』『比较符』『运算数』这样的顺序定义的,没有特例。

Output

对于第 iii 个条件,输出其与前 i−1i-1i1个条件是否存在交集非空的情况。如果不存在交集非空的其他条件,输出一行字符串:『unique』。否则按照从小到大的顺序输出与其存在非空交集的条件的编号,编号之间用空格分隔,最后一个编号末尾不加空格。各条件从1−N1-N1N编号。

Sample Input
4a < 100c > 99b > 100 , b == 99 , c < 98a < 1000, a >= 99
Sample Output
unique1unique1 2



暴力,无他解法


#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define INF 10005struct g{int flag;int sum;char one[40][40];int tw1[40], tw2[40];int three[40];g(){flag = 0;sum = 0;for (int i = 1; i <= 35; i++){tw1[i] = -INF;tw2[i] = INF;}}}a[1010];void Union(char temp2[40], int temp4, int i, int x){if (strcmp(temp2, "==") == 0){if (temp4 >= a[x].tw1[i] && temp4 <= a[x].tw2[i])a[x].tw1[i] = a[x].tw2[i] = temp4;elsea[x].flag = 1;}else if (strcmp(temp2, ">") == 0){temp4++;if (temp4 >= a[x].tw1[i] && temp4 <= a[x].tw2[i])a[x].tw1[i] = temp4;else if (temp4<a[x].tw1[i]);elsea[x].flag = 1;}else if (strcmp(temp2, ">=") == 0){if (temp4 >= a[x].tw1[i] && temp4 <= a[x].tw2[i])a[x].tw1[i] = temp4;else if (temp4<a[x].tw1[i]);elsea[x].flag = 1;}else if (strcmp(temp2, "<") == 0){temp4--;if (temp4 >= a[x].tw1[i] && temp4 <= a[x].tw2[i])a[x].tw2[i] = temp4;else if (temp4>a[x].tw2[i]);elsea[x].flag = 1;}else{if (temp4 >= a[x].tw1[i] && temp4 <= a[x].tw2[i])a[x].tw2[i] = temp4;else if (temp4>a[x].tw2[i]);elsea[x].flag = 1;}}void deal(char temp1[40], char temp2[40], char temp3[40], int x, int num){int len = strlen(temp3), i;int temp4 = 0;i = 0;if (temp3[0] == '-')i = 1;for (; i<len; i++)temp4 = 10 * temp4 + temp3[i] - '0';if (temp3[0] == '-')temp4 *= -1;for (i = 1; i <= num; i++)if (strcmp(a[x].one[i], temp1) == 0){Union(temp2, temp4, i, x);break;}if (i == num + 1){a[x].sum++;strcpy(a[x].one[i], temp1);Union(temp2, temp4, i, x);}}void ans(int x){int arg[1010], l = 0, sign;if (a[x].flag == 1){printf("unique\n");return;}for (int i = 1; i<x; i++){sign = 0;if (a[i].flag == 1)continue;for (int j = 1; j <= a[x].sum; j++){for (int k = 1; k <= a[i].sum; k++)if (strcmp(a[x].one[j], a[i].one[k]) == 0)if (a[x].tw2[j]<a[i].tw1[k] || a[i].tw2[k]<a[x].tw1[j]){sign = 1;break;}if (sign == 1)break;}if (sign == 0)arg[l++] = i;}if (l == 0){printf("unique\n");return;}else{for (int i = 0; i<l; i++)if (i == l - 1)printf("%d\n", arg[i]);elseprintf("%d ", arg[i]);}}int main(){int n, ph;char b[300], temp1[40], temp2[40], temp3[40];cin >> n;getchar();for (int i = 1; i <= n; i++){gets(b);ph = 0;int len = strlen(b);for (int j = 0; j<len;){if (b[j] == ' ' || b[j] == ','){j++;continue;}if (ph == 0){int k = 0;while (j<len && b[j] != ' ' && b[j] != ',')temp1[k++] = b[j++];temp1[k] = '\0';ph = 1;}else if (ph == 1){int k = 0;while (j<len && b[j] != ' ' && b[j] != ',')temp2[k++] = b[j++];temp2[k] = 0;     ph = 2;}else{int k = 0;while (j<len && b[j] != ' ' && b[j] != ',')temp3[k++] = b[j++];temp3[k] = 0;ph = 0;if (a[i].flag == 0)deal(temp1, temp2, temp3, i, a[i].sum);}}ans(i);}return 0;}


1 0
原创粉丝点击