题目链接:http://codeforces.com/contest/551/problem/B

题目大意:给你三个字符串,s1,s2,s3.  s1任意两个字符串之间可以互相交换。

问,在s1中s2和s3在不重叠的情况下,两个字符串出现的最多次数之和的时候为多少,然后将排完序的字符串输出。

思路:一开始打算用桶排做,将每个字符串中的字符出现的次数记录下来,然后查看每个字符串在s1中出现的次数最多为多少,然后按照最大的输出。结果交上去错了,后来才发现是求s1和s2出现的次数之和的最大值,有可能有如下情况,s1出现的最大次数为3,当s1出现次数为3时,s2的次数为1.但是,当s1出现的次数为2时,这个时候s2的次数为5,显然 3+1 < 2+5 。这个时候就应该先输出2*s1和5*s2.然后又开始改,交上去有一组样例tle了,然后过了断断续续将近两个小时才发下错在哪里,具体原因在下面的代码中解释。

#include<bits/stdc++.h>

using namespace std;

# define inf 0x3f3f3f3f

string s1,s2,s3;

int  index;

int d1,d2;

int s;

int len;

map<char,int>q1;

map<char,int>q2;

map<char,int>q3;//桶排

map<char,int >com;

void judge()

{

    int len=s2.size();

    d1=inf;

    for(int i=0; i<len; i++)

    {

        d1=min(d1,q1[s2[i]]/q2[s2[i]]);//这个地方要注意,和d1比较的是 (q1[s2[i]]/q2[s2[i]]),不能只是比较(d1和q1[s2[i]]),因为s2有可能是由重复字母组成的比如说 s1是‘aaa',s2是'aa',这个时候,如果按照后者的计算的话,答案是3,但是正确答案应该是1.

        //cout<<d1<<endl;

    }

    s=-1;

    int t1,t2;

    index=0;

    for(int j=0; j<=d1; j++)

    {

        t1=0,t2=0;

        t1=j;

        int d=inf;

        for(int i=0; i<26; i++)//这里就是tle的原因,一开始我是按照 s3的长度开始跑的,结果一直tle,在看别人的代码的时候,突然想了一下,如果按照最大的来算的话,s2在s1中出现的次数为十的五次方,如果s3的长度也是十的五次方,这个时候耗时就成了10的十次方了,肯定会超时。

        {

            char temp=char(i+'a');

            if(q3[temp])

            {

                d=min(d,(q1[temp]-j*(q2[temp]))/q3[temp]);

            }

        }

        t2=d;

        if(t1+t2>s)

        {

            s=t1+t2;

            index=t1;

        }

    }

}

int main()

{

    while(cin>>s1>>s2>>s3)

    {

        q1.clear();

        q2.clear();

        q3.clear();

        for(int i=0; i<26; i++)

        {

            char s=char(i+'a');

            // cout<<s<<endl;

            q1[s]=0;

            q2[s]=0;

            q3[s]=0;

        }

        for(int i=0; s1[i]; i++)q1[s1[i]]++;

        for(int i=0; s2[i]; i++)q2[s2[i]]++;

        for(int i=0; s3[i]; i++)q3[s3[i]]++;

        judge();

        for(int i=0; i<index; i++)

        {

            cout<<s2;

        }

        len=s2.size();

        for(int i=0; i<len; i++)

        {

            q1[s2[i]]-=index;

        }

        for(int i=1; i<=s-index; i++)

        {

            cout<<s3;

        }

        len=s3.size();

        int g=s-index;

        for(int i=0; i<len; i++)

        {

            q1[s3[i]]-=g;

        }

        len=s1.size();

        for(int i=0; i<len; i++)

        {

            if(q1[s1[i]])

            {

                cout<<s1[i];

                q1[s1[i]]--;

            }

        }

        cout<<endl;

    }

    return 0;

}

B. ZgukistringZ的更多相关文章

  1. Codeforces Round #307 (Div. 2) B. ZgukistringZ 暴力

    B. ZgukistringZ Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/probl ...

  2. 字符串处理/贪心 Codeforces Round #307 (Div. 2) B. ZgukistringZ

    题目传送门 /* 题意:任意排列第一个字符串,使得有最多的不覆盖a/b字符串出现 字符串处理/贪心:暴力找到最大能不覆盖的a字符串,然后在b字符串中动态得出最优解 恶心死我了,我最初想输出最多的a,再 ...

  3. 「日常训练」ZgukistringZ(Codeforces Round #307 Div. 2 B)

    题意与分析(CodeForces 551B) 这他妈哪里是日常训练,这是日常弟中弟. 题意是这样的,给出一个字符串A,再给出两个字符串B,C,求A中任意量字符交换后(不限制次数)能够得到的使B,C作为 ...

  4. 【19.46%】【codeforces 551B】ZgukistringZ

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. Codeforces Round #307 (Div. 2) B. ZgukistringZ

    Professor GukiZ doesn't accept string as they are. He likes to swap some letters in string to obtain ...

  6. Codeforces Round #307 (Div. 2)

    A. GukiZ and Contest time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

随机推荐

  1. codeforces631B

    Print Check CodeForces - 631B Kris works in a large company "Blake Technologies". As a bes ...

  2. ELK--filebeat命令行参数解释

    ./filebeat 使用-c命令行上的标志设置要加载的配置文件,可以通过重复-c标志来指定多个配置文件, 可以使用覆盖个别设置-E <setting>=<value>.< ...

  3. CF-Contest339-614

    614A-Link/Cut Tree 比较水,注意64位int仍然可能溢出. #include <cstdio> #include <algorithm> #include & ...

  4. matplotlib极坐标方法详解

    一.极坐标 在平面内取一个定点O,叫极点,引一条射线Ox,叫做极轴,再选定一个长度单位和角度的正方向(通常取逆时针方向).对于平面内任何一点M,用ρ表示线段OM的长度(有时也用r表示),θ表示从Ox到 ...

  5. 【BZOJ2302】[HAOI2011]Problem C(动态规划)

    [BZOJ2302][HAOI2011]Problem C(动态规划) 题面 BZOJ 洛谷 题解 首先如果\(m=0\)即没有特殊限制的话,那么就和这道题目基本上是一样的. 然而这题也有属于这题的性 ...

  6. HGOI 20180224 题解

    /* The Most Important Things: ljc chat with fyh on QQTa说期末考Ta数学74分感觉不好但是我觉得fyh是地表最强的鸭~~(of course en ...

  7. Linux中禁用命令历史记录

    关闭history记录功能 set +o history 打开history记录功能 set -o history 清空记录 history -c 记录被清空,重新登录后恢复. rm -f $HOME ...

  8. HDU 3338 Kakuro Extension (网络流,最大流)

    HDU 3338 Kakuro Extension (网络流,最大流) Description If you solved problem like this, forget it.Because y ...

  9. poj 3678(SCC+2-SAT)

    传送门:Problem 3678 https://www.cnblogs.com/violet-acmer/p/9769406.html 难点: 题意理解+构图 题意: 有n个点 v[0,2..... ...

  10. 洛谷【P1523】旅行商的背包(算法导论 15-1) 题解

    P1523 旅行商简化版 题目背景 欧几里德旅行商\((Euclidean Traveling Salesman)\)问题也就是货郎担问题一直是困扰全世界数学家.计算机学家的著名问题.现有的算法都没有 ...