UVALIVE 4887 Soccer && UVELIVE 4882

2015 UESTC Winter Training #6 训练赛的两道搜索模拟题


uvalive 4882

//      whn6325689
//		Mr.Phoebe
//		http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")


using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

const int MAXN=1010;

char str[MAXN];

//Íâ³ËÇÒÄÚ·Ç³Ë 

string dfs(string s, int &g)
{
	string res = "", s1 = "", s2;
	int len = s.length(), g2, t = 2, i = 0;
	g = 2;
	while(i < len)
	{
		if(s[i] == '(')
		{
			int f = 0;
			s2 = "";
			do
			{
				if(s[i] == '(')
				{
					if(f++)
						s2 += s[i];
				}
				else if(s[i] == ')')
				{
					if(--f)
						s2 += s[i];
				}
				else
					s2 += s[i];
				i++;
			}
			while(i < len && f);
			s2 = dfs(s2, g2);
			//cout << s2 << endl;
			if(t == 2)
			{
				s1 = s2;
				t = g2;
			}
			else
			{
				if(!g2)
					s2 = '(' + s2 + ')';
				if(t)
					s1 += s2;
				else
				{
					s1 = '(' + s1 + ')' + s2;
					t = 1;
				}
			}
		}
		else if(s[i] == '+')
		{
			res += s1 + '+';
			g = 0;
			s1 = "";
			t = 2;
			i++;
		}
		else
		{
			while(i < len && s[i] != '(' && s[i] != '+')
			{
				if(t == 2)
				{
					s1 += s[i];
					t = 1;
				}
				else if(t)
					s1 += s[i];
				else
				{
					s1 = '(' + s1 + ')' + s[i];
					t = 1;
				}
				i++;
			}
		}
	}
	if(res == "")
		g = t;
	g = g > 0;
	return res + s1;
}

int main()
{
//	freopen("data.txt","r",stdin);
	while(~scanf("%s",str))
	{
		int j;
		printf("%s\n",dfs(string(str),j).c_str());
	}
	return 0;
}

uvalive 4887

//      whn6325689
//		Mr.Phoebe
//		http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")


using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

map<string,int > id;
vi l,r;
char s[222],ss[222];

int n;
int base[22],rank[22],st[22],tob[22];
int mx[22],mi[22];
string name[22];

bool cmp(int x,int y)
{
	return tob[x]>tob[y];
}

void dfs(int dep)
{
	if(dep==l.size())
	{
		for(int i=0;i<=n;i++)
		{
			rank[i]=i;
			tob[i]=base[i];
		}
		for(int i=0;i<l.size();i++)
		{
			if(!st[i])
			{
				tob[l[i]]++;
				tob[r[i]]++;
			}
			else if(st[i]==1)
				tob[l[i]]+=3;
			else
				tob[r[i]]+=3;
		}
		sort(rank+1,rank+n+1,cmp);
		int cur=1;
		for(int i=1;i<=n;i++)
		{
			if(tob[rank[i]]!=tob[rank[i-1]])
				cur=i;
			mx[rank[i]]=max(mx[rank[i]],cur);
			mi[rank[i]]=min(mi[rank[i]],cur);
		}
	}
	else
	{
		st[dep]=0;
		dfs(dep+1);
		st[dep]=1;
		dfs(dep+1);
		st[dep]=2;
		dfs(dep+1);
	}
}


void pu(int x)
{
    if (x == 1)
        printf("1st");
    else if (x == 2)
        printf("2nd");
    else if (x == 3)
        printf("3rd");
    else
        printf("%dth", x);
}



int main()
{
//	freopen("data.txt","r",stdin);
	int m,a,b,x,y,len,cas=0;
	while(read(n)&&read(m)&&(n+m))
	{
		if(cas++)
			putchar('\n');
		id.clear();l.clear();r.clear();
		CLR(base,0);base[0]=-1;
		for(int i=1;i<=n;i++)
		{
			cin>>name[i];
			id[name[i]]=i;
		}
		while(m--)
		{
			scanf("%s vs %s %d %d",s,ss,&a,&b);
			len=strlen(ss);
			ss[len-1]='\0';
//			cout<<s<<" "<<ss<<endl;
			x=id[s];y=id[ss];
			if(~a)
			{
				if(a==b)
				{
					base[x]++;base[y]++;
				}
				else if(a>b)
					base[x]+=3;
				else
					base[y]+=3;
			}
			else
			{
				l.pb(x);r.pb(y);
			}
		}
		CLR(mi,INF);CLR(mx,-INF);
		dfs(0);
		for (int i=1;i<=n;i++)
        {
            printf("Team %s can finish as high as ",name[i].c_str());
            pu(mi[i]);
            printf(" place and as low as ");
            pu(mx[i]);
            puts(" place.");
        }
	}
	return 0;
}


你可能感兴趣的:(UVALIVE 4887 Soccer && UVELIVE 4882)