ABC320C题求助
  • 板块灌水区
  • 楼主idgg007
  • 当前回复1
  • 已保存回复1
  • 发布时间2023/9/16 22:39
  • 上次更新2023/11/2 19:56:17
查看原帖
ABC320C题求助
297831
idgg007楼主2023/9/16 22:39

花式寄掉

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int M,Ans=0x3f3f3f3f;
int cnt[256];
int total_p[100];
vector<string>S;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> M;
    S.resize(3);
    for (int i = 0; i < 3; i++)
    {
        cin >> S[i];
        for (auto &&j : S[i])
        {
            cnt[unsigned(j)]=(cnt[unsigned(j)]==i?i+1:cnt[unsigned(j)]);
        }
    }
    for (short i = 0; i < 256; i++)
    {
        if (cnt[i] == 3)
        {
            int MAXtotal=0;
            int position[3]={-1,-1,-1},mp;
            bool covered[3]={0,0,0};
            for (size_t j = 0; j < 3; j++)
            {
                for (int k = S[j].size() - 1; k >= 0; k--)
                {
                    if (S[j][k] == i)
                    {
                        bool continue_or_break=true;
                        for (size_t e = 0; e < j; e++)
                        {
                            if (k==position[e])
                            {
                                continue_or_break = false;
                                break;
                            }
                        }
                        if (continue_or_break == true||(position[j]==-1||covered[j]==false))
                        {
                            covered[j]=continue_or_break;
                            position[j] = k;
                        }
                    }
                }
            }
            for (int j = 0; j < 3; j++)
            {
                total_p[position[j]]++;
                MAXtotal=max(total_p[position[j]],MAXtotal);
                mp=(MAXtotal==total_p[position[j]]?position[j]:mp);
            }
            if (MAXtotal>1)
            {
                Ans=min((MAXtotal-1)*M+mp,Ans);
            }else{
                Ans=min(max(position[1],max(position[0],position[2])),Ans);
            }
            for (int j = 0; j < 3; j++)
            {
                total_p[position[j]]=0;
            }
        }
    }
    cout<<(Ans==0x3f3f3f3f?-1:Ans)<<endl;
    return 0;
}
2023/9/16 22:39
加载中...