Architect of Your Own Fortune

本文介绍了一种解决幸运票号配对问题的算法实现,通过二分图匹配方法找到最大数量的超级幸运组合,即两个六位数票号前三位之和等于后三位之和的配对方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

J. Architect of Your Own Fortune

Vasya is a schoolboy who rides a trolley bus and then a bus to get to school. He’s always very happy to get a “lucky ticket,” which means the total of the first three digits in the ticket number equals the total of the last three digits. However, Vasya has been down on his luck ever since the beginning of the new school year – over the past month, he hardly had any lucky tickets at all. Vasya thought this over and decided to take control of the situation. Upon reviewing his recent tickets, he realized he can produce several lucky numbers by combining trolley bus tickets with bus tickets. To that end, the schoolboy had to take two tickets, fold them in half along the vertical axis and join halves of different tickets together. The first three digits of the new “super lucky ticket” are the three digits on the lefthand side of the ticket for one mode of transport, and the last three digits are the three digits on the right-hand side of the ticket for the other mode of transport. Example: let us assume that Vasya has a trolley bus ticket with the number 123456 and a bus ticket with the number 789222. They can be combined either as 123222 or as 789456. The first one of these two combinations is super lucky. Your task is to write a program that will help produce the maximum number of super lucky combinations from the tickets available, assuming each ticket can only be used in one combination. Limitations 1 ≤ n, m ≤ 100. Input The first line of the input file contains two integer numbers n and m separated by a space. The second line of the input file contains n six-digit integer numbers separated by a space, representing numbers of bus tickets. The third line of the input file contains m six-digit integer numbers separated by a space, representing numbers of trolley bus tickets. Output The first line of the output file must contain integer number k – the maximum possible number of super lucky combinations. The following k lines must contain these super lucky combinations. Each line with a super lucky combination must start with the Latin letters “AT” if the combination begins with numbers from a bus ticket and ends with numbers from a trolley bus ticket; otherwise the line must start with the letters “TA.” The letters must be followed by two six-digit numbers separated by a space, representing the numbers of the relevant tickets.

 Input.txt 

2 2

123456 111222

141204 555000

out

2

TA 555000 123456

TA 141204 111222

二分图匹配 

之前看着模板做过一个题 这次没有模板 根据二分图的思想 改了好多次居然过了

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long ll;
int a[110][3];
int b[110][3];
int va[110];
int vb[110];
int p1[110];
int p2[110];
int vis[110];
char sn[110][10];
char sm[110][10];
int n,m;
int dfs(int x)
{
    int i;
    for(i=1;i<=m;i++)
    {
        if(a[x][1] == b[i][2]&&vb[i]==0)
        {
            p1[x] = i;
            p2[i] = x;
            vb[i] = 1;
            //cout<<x<<" "<<i<<endl;
            return 1;
        }
        if(a[x][2] == b[i][1]&&vb[i]==0)
        {
            p1[x] = i;
            p2[i] = x;
            vb[i] = 1;
            //cout<<x<<" " <<i<<endl;
            return 1;
        }
    }
    for(i=1;i<=m;i++)
    {
        if(a[x][1] == b[i][2]&&p1[x] != i)
        {
            if(vis[p2[i]]==0) vis[p2[i]] = 1;
            else continue;
            if(dfs(p2[i]))
            {
                //cout<<p2[i]<<endl;
                p1[x] = i;
                p2[i] = x;
                vb[i] = 1;
                return 1;
            }
        }
        if(a[x][2] == b[i][1]&&p1[x] != i)
        {
            if(vis[p2[i]]==0) vis[p2[i]] = 1;
            else continue;
            if(dfs(p2[i]))
            {
                p1[x] = i;
                p2[i] = x;
                vb[i] = 1;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int i,j,x;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++)
    {
        scanf("%s",sn[i]);
        x = 0;
        x += sn[i][0] - '0';
        x += sn[i][1] - '0';
        x += sn[i][2] - '0';
        a[i][1] = x;
        x = 0;
        x += sn[i][3] - '0';
        x += sn[i][4] - '0';
        x += sn[i][5] - '0';
        a[i][2] = x;
    }
    for(i=1;i<=m;i++)
    {
        scanf("%s",sm[i]);
        x = 0;
        x += sm[i][0] - '0';
        x += sm[i][1] - '0';
        x += sm[i][2] - '0';
        b[i][1] = x;
        x = 0;
        x += sm[i][3] - '0';
        x += sm[i][4] - '0';
        x += sm[i][5] - '0';
        b[i][2] = x;
    }
    memset(va,0,sizeof(va));
    memset(vb,0,sizeof(vb));
    memset(p1,0,sizeof(p1));
    memset(p2,0,sizeof(p2));
    for(i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        dfs(i);
    }
    int ans = 0;
    for(i=1;i<=n;i++)
    {
        if(p1[i] != 0) ans++;
    }
    printf("%d\n",ans);
    for(i=1;i<=n;i++)
    {
        if(p1[i] != 0)
        {
          //  cout<<p1[i]<<endl;
            if(a[i][1]==b[p1[i]][2])
            {
                printf("AT %s %s\n",sn[i],sm[p1[i]]);
            }
            else
            {
                printf("TA %s %s\n",sm[p1[i]],sn[i]);
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值