uva127

数据结构线性表,写起来就是蛋疼。。。

下面是代码,用vector写的2.7飘过,还是数组快

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=60;
vector<vector<string> > grid(maxn,vector<string>(maxn));
int cnt[maxn];
int x,y;
bool process(int i)
{
    int num=0,pre=1;
    x=-1,y=-1;
    for(int j=i-1; j>=1; j--)
    {
        if(cnt[j]>0) num++;
        //num++;
        if(num==1&&pre)
        {
            x=j;
            pre=0;
        }
        if(num==3)
        {
            y=j;
            break;
        }
    }
    if(x>0||y>0)
    return true;
    return false;
}
bool Match(string a,string b)
{
    return a[0]==b[0]||a[1]==b[1];
}
bool exchange(int tmp,int pos)
{
    if(tmp<=0) return false;
    if(Match(grid[tmp][cnt[tmp]-1],grid[pos][cnt[pos]-1]))
    {
        grid[tmp][cnt[tmp]]=grid[pos][cnt[pos]-1];
        cnt[pos]--;
        cnt[tmp]++;
        return true;
    }
    return false;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
#endif
    string a;
    while(cin>>a)
    {
        if(a=="#")
            break;
        memset(cnt,0,sizeof(cnt));
        grid[1][0]=a;
        cnt[1]++;
        for(int i=2; i<=52; i++)
        {
            cin>>grid[i][cnt[i]++];
        }
        for(int i=2; i<=52;)
        {
            x=-1,y=-1;
            if(cnt[i]==0)
            {
                i++;
                continue;
            }
            if(!process(i))
            {
                i++;
                continue;
            }
            if(y>0)
            {
                if(y>0&&Match(grid[y][cnt[y]-1],grid[i][cnt[i]-1]))
                {
                    grid[y][cnt[y]]=grid[i][cnt[i]-1];
                    cnt[i]--;
                    cnt[y]++;
                    i=y;
                    continue;
                }
            }
            if(x>0)
            {
                if(x>0&&Match(grid[x][cnt[x]-1],grid[i][cnt[i]-1]))
                {
                    grid[x][cnt[x]]=grid[i][cnt[i]-1];
                    cnt[i]--;
                    cnt[x]++;
                    i=x;
                    continue;
                }
            }
            i++;
        }
        int ans=0;
        for(int i=1; i<=52; i++)
            if(cnt[i]) ans++;
        ans>1?printf("%d piles remaining: ",ans):printf("%d pile remaining: ",ans);
        bool first=true;
        for(int i=1; i<=52; i++)
        {
            if(cnt[i]&&first)
            {
                printf("%d",cnt[i]);
                first=false;
            }
            else if(cnt[i])printf(" %d",cnt[i]);
        }
        cout<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值