PAT (Advanced Level) Practice - 1045 Favorite Color Stripe(30 分)

简介: PAT (Advanced Level) Practice - 1045 Favorite Color Stripe(30 分)

题目链接:点击打开链接

题目大意:略。

解题思路:找规律:row,col 都从 1 开始,外围为 0;当前位置填入的值val = max(相邻左,相邻上),如果 like[i] == give[j],val = ++ma,否认 val = ma。


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

注意:WA 代码 与 AC 代码区别是:WA代码先手动初始化(1,1)开始,再自动填充;而AC代码是利用外围的(0,0)直接自动填充;如图所示有一种情况WA代码(码如其名):

image.png

image.png

AC 代码

#include<bits/stdc++.h>#include<cmath>#define mem(a,b) memset(a,b,sizeof a)#define ssclr(ss) ss.clear(), ss.str("")#define INF 0x3f3f3f3f#define MOD 1000000007usingnamespacestd;
typedeflonglongll;
constintM=210, L=1e4+10;
intlike[M], give[L], rsrr[M][L];
intmain()
{
intn,m,l,ma;
scanf("%d%d",&n,&m);
for(inti=1;i<=m;i++) scanf("%d",&like[i]);
scanf("%d",&l);
for(inti=1;i<=l;i++) scanf("%d",&give[i]);
for(inti=1;i<=m;i++)
    {
for(intj=1;j<=l;j++)
        {
ma=max(rsrr[i-1][j],rsrr[i][j-1]);
if(like[i]==give[j]) rsrr[i][j]=++ma;
elsersrr[i][j]=ma;
        }
    }
printf("%d\n",ma);
return0;
}

WA 代码

#include<bits/stdc++.h>#include<cmath>#define mem(a,b) memset(a,b,sizeof a)#define ssclr(ss) ss.clear(), ss.str("")#define INF 0x3f3f3f3f#define MOD 1000000007usingnamespacestd;
typedeflonglongll;
constintM=210, L=1e4+10;
intlike[M], give[L], rsrr[M][L];
intmain()
{
intn,m,l,ma;
scanf("%d%d",&n,&m);
for(inti=1;i<=m;i++) scanf("%d",&like[i]);
scanf("%d",&l);
for(inti=1;i<=l;i++) scanf("%d",&give[i]);
intth=0;
for(inti=1;i<=l;i++)
if(like[1]==give[i]) rsrr[1][i]=++th;
elsersrr[1][i]=th;
for(inti=1;i<=m;i++) rsrr[i][1]=rsrr[1][1];
for(inti=2;i<=m;i++)
    {
for(intj=2;j<=l;j++)
        {
ma=max(rsrr[i-1][j],rsrr[i][j-1]);
if(like[i]==give[j]) rsrr[i][j]=++ma;
elsersrr[i][j]=ma;
        }
    }
printf("%d\n",ma);
return0;
}
目录
相关文章
PAT (Advanced Level) Practice - 1091 Acute Stroke(30 分)
PAT (Advanced Level) Practice - 1091 Acute Stroke(30 分)
85 0
PAT (Advanced Level) Practice - 1135 Is It A Red-Black Tree(30 分)
PAT (Advanced Level) Practice - 1135 Is It A Red-Black Tree(30 分)
81 0
PAT (Advanced Level) Practice - 1053 Path of Equal Weight(30 分)
PAT (Advanced Level) Practice - 1053 Path of Equal Weight(30 分)
105 0
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
PAT (Advanced Level) Practice - 1026 Table Tennis(30 分)
112 0
|
算法
PAT (Advanced Level) Practice - 1033 To Fill or Not to Fill(25 分)
PAT (Advanced Level) Practice - 1033 To Fill or Not to Fill(25 分)
76 0
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
PAT (Advanced Level) Practice - 1095 Cars on Campus(30 分)
115 0
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
PAT (Advanced Level) Practice - 1147 Heaps(30 分)
101 0
|
存储
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
PAT (Advanced Level) Practice - 1126 Eulerian Path(25 分)
119 0
|
C++
PAT (Advanced Level) Practice - 1114 Family Property(25 分)
PAT (Advanced Level) Practice - 1114 Family Property(25 分)
77 0
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
PAT (Advanced Level) Practice - 1127 ZigZagging on a Tree(30 分)
106 0