PTA----情人节专场

大提琴的声音就像一条河,左岸是我无法忘却的回忆,右岸是我值得紧握的璀璨年华,中间流淌的,是我年年岁岁淡淡的感伤。

                                          L1-1 PTA使我精神焕发 (5分)

PTA使我精神焕发.jpg

以上是湖北经济学院同学的大作。本题就请你用汉语拼音输出这句话。

输入格式:

本题没有输入。

输出格式:

在一行中按照样例输出,以惊叹号结尾。

输入样例:

输出样例:

PTA shi3 wo3 jing1 shen2 huan4 fa1 !
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int main(){
    cout<<"PTA shi3 wo3 jing1 shen2 huan4 fa1 !"<<endl;
    return 0;
}

                                                     L1-2 6翻了 (15分)

666.JPG

“666”是一种网络用语,大概是表示某人很厉害、我们很佩服的意思。最近又衍生出另一个数字“9”,意思是“6翻了”,实在太厉害的意思。如果你以为这就是厉害的最高境界,那就错啦 —— 目前的最高境界是数字“27”,因为这是 3 个 “9”!

本题就请你编写程序,将那些过时的、只会用一连串“6666……6”表达仰慕的句子,翻译成最新的高级表达。

输入格式:

输入在一行中给出一句话,即一个非空字符串,由不超过 1000 个英文字母、数字和空格组成,以回车结束。

输出格式:

从左到右扫描输入的句子:如果句子中有超过 3 个连续的 6,则将这串连续的 6 替换成 9;但如果有超过 9 个连续的 6,则将这串连续的 6 替换成 27。其他内容不受影响,原样输出。

输入样例:

it is so 666 really 6666 what else can I say 6666666666

输出样例:

it is so 666 really 9 what else can I say 27
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
string s;
int main()
{
    getline(cin, s);
    int l=s.length();
    for(int i=0; i<l; i++)
    {
        int t=0;
        if(s[i]=='6')
        {
            for(int j=i; j<l; j++)
            {
                if(s[j]=='6')
                    t++;
                else
                    break;
            }
            if(t>3&&t<=9)
            {
                printf("9");
                i+=t-1;
                continue;
            }
            else if(t>9)
            {
                printf("27");
                i+=t-1;
                continue;
            }
        }
        printf("%c",s[i]);
    }
    return 0;
}

                                                    L1-3 敲笨钟 (20分)

微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜身体早点睡觉。为了增加敲钟的趣味性,还会糟改几句古诗词。其糟改的方法为:去网上搜寻压“ong”韵的古诗词,把句尾的三个字换成“敲笨钟”。例如唐代诗人李贺有名句曰:“寻章摘句老雕虫,晓月当帘挂玉弓”,其中“虫”(chong)和“弓”(gong)都压了“ong”韵。于是这句诗就被糟改为“寻章摘句老雕虫,晓月当帘敲笨钟”。

现在给你一大堆古诗词句,要求你写个程序自动将压“ong”韵的句子糟改成“敲笨钟”。

输入格式:

输入首先在第一行给出一个不超过 20 的正整数 N。随后 N 行,每行用汉语拼音给出一句古诗词,分上下两半句,用逗号 , 分隔,句号 . 结尾。相邻两字的拼音之间用一个空格分隔。题目保证每个字的拼音不超过 6 个字符,每行字符的总长度不超过 100,并且下半句诗至少有 3 个字。

输出格式:

对每一行诗句,判断其是否压“ong”韵。即上下两句末尾的字都是“ong”结尾。如果是压此韵的,就按题面方法糟改之后输出,输出格式同输入;否则输出 Skipped,即跳过此句。

输入样例:

5
xun zhang zhai ju lao diao chong, xiao yue dang lian gua yu gong.
tian sheng wo cai bi you yong, qian jin san jin huan fu lai.
xue zhui rou zhi leng wei rong, an xiao chen jing shu wei long.
zuo ye xing chen zuo ye feng, hua lou xi pan gui tang dong.
ren xian gui hua luo, ye jing chun shan kong.

输出样例:

xun zhang zhai ju lao diao chong, xiao yue dang lian qiao ben zhong.
Skipped
xue zhui rou zhi leng wei rong, an xiao chen jing qiao ben zhong.
Skipped
Skipped
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int main()
{
    int n,flag,t;
    char str[105];
    char s[20]="qiao ben zhong.";
    cin>>n;
    getchar();
    while(n--)
    {
        cin.getline(str,10000,'\n');
        int len=strlen(str);
        flag=1;
        for(int i=0; i<len; i++)
        {
            if(str[i]==','||str[i]=='.')
            {
                if(flag&&str[i-3]=='o'&&str[i-2]=='n'&&str[i-1]=='g')
                    flag=1;
                else
                    flag=0;
            }
        }
        if(flag==1)
        {
            t=3;
            for(int i=len-1; i>=0; i--)
            {
                if(str[i]==' ')
                {
                    t--;
                    if(t==0)
                    {
                        str[i+1]='\0';
                        cout<<str<<s<<endl;
                        break;
                    }
                }
            }
        }
        else
            cout<<"Skipped"<<endl;
    }
    return 0;
}

                                        L1-4 心理阴影面积 (5分)

xlyy.JPG

这是一幅心理阴影面积图。我们都以为自己可以匀速前进(图中蓝色直线),而拖延症晚期的我们往往执行的是最后时刻的疯狂赶工(图中的红色折线)。由红、蓝线围出的面积,就是我们在做作业时的心理阴影面积。

现给出红色拐点的坐标 (x,y),要求你算出这个心理阴影面积。

输入格式:

输入在一行中给出 2 个不超过 100 的正整数 x 和 y,并且保证有 x>y。这里假设横、纵坐标的最大值(即截止日和最终完成度)都是 100。

输出格式:

在一行中输出心理阴影面积。

友情提醒:三角形的面积 = 底边长 x 高 / 2;矩形面积 = 底边长 x 高。嫑想得太复杂,这是一道 5 分考减法的题……

输入样例:

90 10

输出样例:

4000
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int x,y;
double jg,s1,s2,s3;
const int zs=5000;
int main(){
    cin>>x>>y;
    s1=(x*y*1.0)/2;
    s2=(100-x)*y;
    s3=((100-x)*(100-y)*1.0)/2;
    jg=zs-s1-s2-s3;
    cout<<jg<<endl;
    return 0;
}

                                              L1-5 新胖子公式 (10分)

根据钱江晚报官方微博的报导,最新的肥胖计算方法为:体重(kg) / 身高(m) 的平方。如果超过 25,你就是胖子。于是本题就请你编写程序自动判断一个人到底算不算胖子。

输入格式:

输入在一行中给出两个正数,依次为一个人的体重(以 kg 为单位)和身高(以 m 为单位),其间以空格分隔。其中体重不超过 1000 kg,身高不超过 3.0 m。

输出格式:

首先输出将该人的体重和身高代入肥胖公式的计算结果,保留小数点后 1 位。如果这个数值大于 25,就在第二行输出 PANG,否则输出 Hai Xing

输入样例 1:

100.1 1.74

输出样例 1:

33.1
PANG

输入样例 2:

65 1.70

输出样例 2:

22.5
Hai Xing
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
double w,h,pd;
int main()
{
    cin>>w>>h;
    pd=w/(h*h);
    printf("%.1f\n",pd);
    if(pd>25)
        cout<<"PANG"<<endl;
    else
        cout<<"Hai Xing"<<endl;
    return 0;
}

                                                L1-6 幸运彩票 (15分)

彩票的号码有 6 位数字,若一张彩票的前 3 位上的数之和等于后 3 位上的数之和,则称这张彩票是幸运的。本题就请你判断给定的彩票是不是幸运的。

输入格式:

输入在第一行中给出一个正整数 N(≤ 100)。随后 N 行,每行给出一张彩票的 6 位数字。

输出格式:

对每张彩票,如果它是幸运的,就在一行中输出 You are lucky!;否则输出 Wish you good luck.

输入样例:

2
233008
123456

输出样例:

You are lucky!
Wish you good luck.
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,sz;
int main()
{
    cin>>n;
    while(n--)
    {
        cin>>sz;
        if(sz/100000+sz/10000%10+sz/1000%10==sz%10+sz%100/10+sz%1000/100)
            cout<<"You are lucky!"<<endl;
        else
            cout<<"Wish you good luck."<<endl;
    }
    return 0;
}

                                            L1-7 吃鱼还是吃肉 (10分)

fish.JPG 肉.JPG

国家给出了 8 岁男宝宝的标准身高为 130 厘米、标准体重为 27 公斤;8 岁女宝宝的标准身高为 129 厘米、标准体重为 25 公斤。

现在你要根据小宝宝的身高体重,给出补充营养的建议。

输入格式:

输入在第一行给出一个不超过 10 的正整数 N,随后 N 行,每行给出一位宝宝的身体数据:

性别 身高 体重

其中性别是 1 表示男生,0 表示女生。身高体重都是不超过 200 的正整数。

输出格式:

对于每一位宝宝,在一行中给出你的建议:

  • 如果太矮了,输出:duo chi yu!(多吃鱼);
  • 如果太瘦了,输出:duo chi rou!(多吃肉);
  • 如果正标准,输出:wan mei!(完美);
  • 如果太高了,输出:ni li hai!(你厉害);
  • 如果太胖了,输出:shao chi rou!(少吃肉)。

先评价身高,再评价体重。两句话之间要有 1 个空格。

输入样例:

4
0 130 23
1 129 27
1 130 30
0 128 27

输出样例:

ni li hai! duo chi rou!
duo chi yu! wan mei!
wan mei! shao chi rou!
duo chi yu! shao chi rou!
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,xb,h,w;
int main()
{
    cin>>n;
    getchar();
    while(n--)
    {
        cin>>xb>>h>>w;
        if(xb)
        {
            if(h<130)
                cout << "duo chi yu! " ;
            else if(h>130)
                cout << "ni li hai! " ;
            else
                cout << "wan mei! " ;
            if(w<27)
                cout << "duo chi rou!" ;
            else if(w>27)
                cout << "shao chi rou!" ;
            else
                cout << "wan mei!" ;
        }
        else
        {
            if(h<129)
                cout << "duo chi yu! " ;
            else if(h>129)
                cout << "ni li hai! " ;
            else
                cout << "wan mei! " ;
            if(w<25)
                cout << "duo chi rou!" ;
            else if(w>25)
                cout << "shao chi rou!" ;
            else
                cout << "wan mei!" ;
        }
        cout<<endl;
    }
    return 0;
}

                               L1-8 估值一亿的AI核心代码 (20分)

AI.jpg

以上图片来自新浪微博。

本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:

  • 无论用户说什么,首先把对方说的话在一行中原样打印出来;
  • 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
  • 把原文中所有大写英文字母变成小写,除了 I
  • 把原文中所有独立的 can youcould you 对应地换成 I canI could—— 这里“独立”是指被空格或标点符号分隔开的单词;
  • 把原文中所有独立的 I 和 me 换成 you
  • 把原文中所有的问号 ? 换成惊叹号 !
  • 在一行中输出替换后的句子作为 AI 的回答。

输入格式:

输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。

输出格式:

按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。

输入样例:

6
Hello ?
 Good to chat   with you
can   you speak Chinese?
Really?
Could you show me 5
What Is this prime? I,don 't know

输出样例:

Hello ?
AI: hello!
 Good to chat   with you
AI: good to chat with you
can   you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don't know
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int N;
int main()
{
    cin>>N;
    getchar();
    while(N--)
    {
        string s,S[1010],s1;
        int cnt=0;
        getline(cin,s);
        cout<<s<<endl;
        cout<<"AI:";
        for(int i=0; i<s.size(); i++)
        {
            if(isalnum(s[i]))
            {
                if(s[i]!='I')
                    s[i]=tolower(s[i]);
            }
            else
            {
                s.insert(i," ");
                i++;
            }
            if(s[i]=='?')
                s[i]='!';
        }
        stringstream ss(s);
        while(ss>>s1)
            S[cnt++]=s1;
        if(!isalnum(S[0][0]))
            cout<<" ";
        for(int i=0; i<cnt; i++)
        {
            if(!isalnum(S[i][0]))
                cout<<S[i];
            else if(S[i]=="can"&&(i+1<cnt&&S[i+1]=="you"))
            {
                cout<<" I can";
                i++;
            }
            else if(S[i]=="could"&&(i+1<cnt&&S[i+1]=="you"))
            {
                cout<<" I could";
                i++;
            }
            else if(S[i]=="I"||S[i]=="me")
                cout<<" you";
            else
                cout<<" "<<S[i];
        }
        cout<<endl;
    }
    return 0;
}

                                           L2-1 特立独行的幸福 (25分)

对一个十进制数的各位数字做一次平方和,称作一次迭代。如果一个十进制数能通过若干次迭代得到 1,就称该数为幸福数。1 是一个幸福数。此外,例如 19 经过 1 次迭代得到 82,2 次迭代后得到 68,3 次迭代后得到 100,最后得到 1。则 19 就是幸福数。显然,在一个幸福数迭代到 1 的过程中经过的数字都是幸福数,它们的幸福是依附于初始数字的。例如 82、68、100 的幸福是依附于 19 的。而一个特立独行的幸福数,是在一个有限的区间内不依附于任何其它数字的;其独立性就是依附于它的的幸福数的个数。如果这个数还是个素数,则其独立性加倍。例如 19 在区间[1, 100] 内就是一个特立独行的幸福数,其独立性为 2×4=8。

另一方面,如果一个大于1的数字经过数次迭代后进入了死循环,那这个数就不幸福。例如 29 迭代得到 85、89、145、42、20、4、16、37、58、89、…… 可见 89 到 58 形成了死循环,所以 29 就不幸福。

本题就要求你编写程序,列出给定区间内的所有特立独行的幸福数和它的独立性。

输入格式:

输入在第一行给出闭区间的两个端点:1<A<B≤10​4​​。

输出格式:

按递增顺序列出给定闭区间 [A,B] 内的所有特立独行的幸福数和它的独立性。每对数字占一行,数字间以 1 个空格分隔。

如果区间内没有幸福数,则在一行中输出 SAD

输入样例 1:

10 40

输出样例 1:

19 8
23 6
28 3
31 4
32 3

注意:样例中,10、13 也都是幸福数,但它们分别依附于其他数字(如 23、31 等等),所以不输出。其它数字虽然其实也依附于其它幸福数,但因为那些数字不在给定区间 [10, 40] 内,所以它们在给定区间内是特立独行的幸福数。

输入样例 2:

110 120

输出样例 2:

SAD
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int pri[10010],mp[10010];
bool pd(int n)
{
    if(n==1)
        return true;
    if(n==4)
        return false;
    int tmp=0;
    while(n>0)
    {
        tmp+=(n%10)*(n%10);
        n/=10;
    }
    mp[tmp]=1;
    return pd(tmp);
}
int dfs(int n,int dep)
{
    if(n==1)
        return dep;
    int tmp=0;
    while(n>0)
    {
        tmp+=(n%10)*(n%10);
        n/=10;
    }
    return dfs(tmp,dep+1);
}
void db()
{
    for(int i=2; i<=10000; i++)
    {
        if(pri[i]==0)
        {
            int j=i;
            while(j*i<=10000)
            {
                pri[i*j]=1;
                j++;
            }
        }
    }
}
int main()
{
    db();
    int a,b;
    int fg=1;
    cin>>a>>b;
    for(int i=a; i<=b; i++)
        if(!pd(i))
            mp[i]=1;
    for(int i=a; i<=b; i++)
    {
        if(mp[i]==0)
        {
            int jg=dfs(i,0);
            if(pri[i]==0)
                jg*=2;
            cout<<i<<" "<<jg<<endl;
            fg=0;
        }
    }
    if(fg)
        cout<<"SAD"<<endl;
    return 0;
}

                                                 L2-2 冰岛人 (25分)

2018年世界杯,冰岛队因1:1平了强大的阿根廷队而一战成名。好事者发现冰岛人的名字后面似乎都有个“松”(son),于是有网友科普如下:

iceland.JPG

冰岛人沿用的是维京人古老的父系姓制,孩子的姓等于父亲的名加后缀,如果是儿子就加 sson,女儿则加 sdottir。因为冰岛人口较少,为避免近亲繁衍,本地人交往前先用个 App 查一下两人祖宗若干代有无联系。本题就请你实现这个 App 的功能。

输入格式:

输入首先在第一行给出一个正整数 N(1<N≤10​5​​),为当地人口数。随后 N 行,每行给出一个人名,格式为:名 姓(带性别后缀),两个字符串均由不超过 20 个小写的英文字母组成。维京人后裔是可以通过姓的后缀判断其性别的,其他人则是在姓的后面加 m 表示男性、f 表示女性。题目保证给出的每个维京家族的起源人都是男性。

随后一行给出正整数 M,为查询数量。随后 M 行,每行给出一对人名,格式为:名1 姓1 名2 姓2。注意:这里的是不带后缀的。四个字符串均由不超过 20 个小写的英文字母组成。

题目保证不存在两个人是同名的。

输出格式:

对每一个查询,根据结果在一行内显示以下信息:

  • 若两人为异性,且五代以内无公共祖先,则输出 Yes
  • 若两人为异性,但五代以内(不包括第五代)有公共祖先,则输出 No
  • 若两人为同性,则输出 Whatever
  • 若有一人不在名单内,则输出 NA

所谓“五代以内无公共祖先”是指两人的公共祖先(如果存在的话)必须比任何一方的曾祖父辈分高。

输入样例:

15
chris smithm
adam smithm
bob adamsson
jack chrissson
bill chrissson
mike jacksson
steve billsson
tim mikesson
april mikesdottir
eric stevesson
tracy timsdottir
james ericsson
patrick jacksson
robin patricksson
will robinsson
6
tracy tim james eric
will robin tracy tim
april mike steve bill
bob adam eric steve
tracy tim tracy tim
x man april mikes

输出样例:

Yes
No
No
Whatever
Whatever
NA
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
struct node
{
    char xb;
    string fa;
};
map<string, node> R;
int judge(string a,string b)
{
    int i=1,j;
    for(string A=a; !A.empty(); A=R[A].fa,i++)
    {
        j=1;
        for(string B=b; !B.empty(); B=R[B].fa,j++)
        {
            if(i>=5&&j>=5)
                return 1;
            if(A==B&&(i<5||j<5))
                return 0;
        }
    }
    return 1;

}
int main()
{
    int n, m;
    string str,a,b;
    cin.sync_with_stdio(false);
    cin>>n;
    for (int i=0; i<n; i++)
    {
        cin>>a>>b;
        if (b.back()=='n')
            R[a]= {'m',b.substr(0,b.size()-4)};
        else if (b.back() == 'r')
            R[a]= {'f',b.substr(0, b.size()-7)};
        else
            R[a].xb=b.back();
    }
    cin>>m;
    for (int i=0; i<m; i++)
    {
        cin>>a>>str>>b>>str;
        if (R.find(a)==R.end()||R.find(b)==R.end())
            printf("NA\n");
        else if(R[a].xb==R[b].xb)
            printf("Whatever\n");
        else
            printf("%s\n", judge(a,b)?"Yes":"No");
    }
    return 0;
}

                                                L2-3 深入虎穴 (25分)

著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报。已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门。每一扇门背后或者是一个房间,或者又有很多条路,同样是每条路通向一扇门…… 他的手里有一张表格,是其他间谍帮他收集到的情报,他们记下了每扇门的编号,以及这扇门背后的每一条通路所到达的门的编号。007 发现不存在两条路通向同一扇门。

内线告诉他,情报就藏在迷宫的最深处。但是这个迷宫太大了,他需要你的帮助 —— 请编程帮他找出距离入口最远的那扇门。

输入格式:

输入首先在一行中给出正整数 N(<10​5​​),是门的数量。最后 N 行,第 i 行(1≤i≤N)按以下格式描述编号为 i 的那扇门背后能通向的门:

K D[1] D[2] ... D[K]

其中 K 是通道的数量,其后是每扇门的编号。

输出格式:

在一行中输出距离入口最远的那扇门的编号。题目保证这样的结果是唯一的。

输入样例:

13
3 2 3 4
2 5 6
1 7
1 8
1 9
0
2 11 10
1 13
0
0
1 12
0
0

输出样例:

12
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,k,t,s,mx,jg,arr[100005],cd[100005],vis[100005];
vector <int> V[100005];
void dfs(int x)
{
    queue<int> Q;
    Q.push(x);
    mx=-1;
    cd[x]=0;
    vis[x]=1;
    jg=x;
    while(!Q.empty())
    {
        int ft=Q.front();
        Q.pop();
        for(int i=0; i<V[ft].size(); i++)
        {
            if(vis[V[ft][i]]==0)
            {
                vis[V[ft][i]]=1;
                cd[V[ft][i]]=cd[ft]+1;
                if(cd[V[ft][i]]>mx)
                {
                    mx = cd[V[ft][i]];
                    jg = V[ft][i];
                }
                Q.push(V[ft][i]);
            }
        }
    }
    return;
}
int main()
{
    cin >> n;
    for(int i=1; i<=n; i++)
    {
        cin >> k;
        for(int j=0; j<k; j++)
        {
            cin>>t;
            arr[t]++;
            V[i].push_back(t);
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(arr[i]==0)
        {
            s=i;
            break;
        }
    }
    dfs(s);
    cout<<jg<<endl;
    return 0;
}

                                                       L2-4 彩虹瓶 (25分)

rb.JPG

彩虹瓶的制作过程(并不)是这样的:先把一大批空瓶铺放在装填场地上,然后按照一定的顺序将每种颜色的小球均匀撒到这批瓶子里。

假设彩虹瓶里要按顺序装 N 种颜色的小球(不妨将顺序就编号为 1 到 N)。现在工厂里有每种颜色的小球各一箱,工人需要一箱一箱地将小球从工厂里搬到装填场地。如果搬来的这箱小球正好是可以装填的颜色,就直接拆箱装填;如果不是,就把箱子先码放在一个临时货架上,码放的方法就是一箱一箱堆上去。当一种颜色装填完以后,先看看货架顶端的一箱是不是下一个要装填的颜色,如果是就取下来装填,否则去工厂里再搬一箱过来。

如果工厂里发货的顺序比较好,工人就可以顺利地完成装填。例如要按顺序装填 7 种颜色,工厂按照 7、6、1、3、2、5、4 这个顺序发货,则工人先拿到 7、6 两种不能装填的颜色,将其按照 7 在下、6 在上的顺序堆在货架上;拿到 1 时可以直接装填;拿到 3 时又得临时码放在 6 号颜色箱上;拿到 2 时可以直接装填;随后从货架顶取下 3 进行装填;然后拿到 5,临时码放到 6 上面;最后取了 4 号颜色直接装填;剩下的工作就是顺序从货架上取下 5、6、7 依次装填。

但如果工厂按照 3、1、5、4、2、6、7 这个顺序发货,工人就必须要愤怒地折腾货架了,因为装填完 2 号颜色以后,不把货架上的多个箱子搬下来就拿不到 3 号箱,就不可能顺利完成任务。

另外,货架的容量有限,如果要堆积的货物超过容量,工人也没办法顺利完成任务。例如工厂按照 7、6、5、4、3、2、1 这个顺序发货,如果货架够高,能码放 6 只箱子,那还是可以顺利完工的;但如果货架只能码放 5 只箱子,工人就又要愤怒了……

本题就请你判断一下,工厂的发货顺序能否让工人顺利完成任务。

输入格式:

输入首先在第一行给出 3 个正整数,分别是彩虹瓶的颜色数量 N(1<N≤10​3​​)、临时货架的容量 M(<N)、以及需要判断的发货顺序的数量 K。

随后 K 行,每行给出 N 个数字,是 1 到N 的一个排列,对应工厂的发货顺序。

一行中的数字都以空格分隔。

输出格式:

对每个发货顺序,如果工人可以愉快完工,就在一行中输出 YES;否则输出 NO

输入样例:

7 5 3
7 6 1 3 2 5 4
3 1 5 4 2 6 7
7 6 5 4 3 2 1

输出样例:

YES
NO
NO
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <ctime>
#include <cctype>
#include <bitset>
#include <utility>
#include <sstream>
#include <complex>
#include <iomanip>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,m,k,x;
int main()
{
    cin>>n>>m>>k;
    while(k--)
    {
        int j=0;
        vector<int> zs,xz;
        for(int i=1; i<=n; i++)
        {
            cin>>x;
            zs.push_back(x);
            xz.push_back(i);
        }
        stack<int> st;
        for(int i=0; i<n; i++)
        {
            if(zs[i]!=xz[j])
            {
                st.push(zs[i]);
                if(st.size()>m)
                    break;
            }
            else
            {
                j++;
                while(st.size()&&st.top()==xz[j])
                {
                    st.pop();
                    j++;
                }
            }
        }
        if(j==n&&st.empty())
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}
发布了804 篇原创文章 · 获赞 42 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_44170305/article/details/104363115