QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#88475 | #4107. 墙上的句子 | LordLaffey | 100 ✓ | 45ms | 3904kb | C++14 | 8.6kb | 2023-03-16 11:56:36 | 2023-03-16 11:56:39 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
namespace OCTANE
{
template <typename T> inline bool read(T &x)
{
x = 0; bool f = 0; char ch = getchar();
if (ch == EOF) return false;
while (ch < '0' || ch > '9')
{
if (ch == '-') f = 1; ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 1) + (x << 3) + (ch^48); ch = getchar();
}
if (f) x = -x; return true;
}
template <typename T> inline void print(T x)
{
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar(x%10 + '0');
}
template <typename T, typename ...TT>
inline void print(T x, char c)
{
print(x); putchar(c);
}
template <typename T, typename ...TT>
inline int read(T &x, TT &...y)
{
return read(x) + read(y...);
}
}using namespace OCTANE;
#define ull unsigned long long
const int SIZE=80;
const int MAXN=3005;
const int N=1e6+5;
const ull Base=233;
const int INF=0x3f3f3f3f;
struct EDGE{
int nxt,v;
int w;
}edge[N];
int head[MAXN],cur[MAXN],cnt=1;
void add_edge(int u,int v,int w){
edge[++cnt].v=v;
edge[cnt].w=w;
edge[cnt].nxt=head[u];
head[u]=cnt;
edge[++cnt].v=u;
edge[cnt].w=0;
edge[cnt].nxt=head[v];
head[v]=cnt;
}
int dis[MAXN];
bool bfs(int s,int t){
memset(dis,0,sizeof(dis));
memcpy(cur,head,sizeof(cur));
queue<int> q;
q.push(s);
dis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i;i=edge[i].nxt)
{
int v=edge[i].v;
if(!dis[v]&&edge[i].w)
{
dis[v]=dis[u]+1;
q.push(v);
}
}
}
return dis[t];
}
int dfs(int u,int flow,int t){
if(u==t||!flow) return flow;
int rest=flow,i;
for(i=cur[u];i;i=edge[i].nxt)
{
int v=edge[i].v;
if(dis[v]==dis[u]+1&&edge[i].w)
{
int k=dfs(v,min(rest,edge[i].w),t);
if(!k)
{
dis[v]=0;
continue;
}
edge[i].w-=k,edge[i^1].w+=k;
rest-=k;
if(!rest) break;
}
}
cur[u]=i;
return flow-rest;
}
int dinic(int s,int t){
int flow=0;
while(bfs(s,t))
flow+=dfs(s,INF,t);
return flow;
}
ull pw[SIZE],H[SIZE],RH[SIZE];
map<ull,int> mp1,mp2;
char s[SIZE][SIZE];
int a[SIZE],b[SIZE];
int rtot[SIZE],ctot[SIZE];
int row[2][SIZE][SIZE],col[2][SIZE][SIZE],big[MAXN];
void clear(){
cnt=1;
memset(big,0,sizeof(big));
memset(head,0,sizeof(head));
memset(rtot,0,sizeof(rtot));
memset(ctot,0,sizeof(ctot));
mp1.clear(),mp2.clear();
}
void solve(){
clear();
int n,m;
read(n,m);
for(int i=1;i<=n;i++)
read(a[i]);
for(int i=1;i<=m;i++)
read(b[i]);
int ans=0,id_tot=0;
for(int i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
for(int j=1;j<=m;j++)
H[j]=H[j-1]*Base+s[i][j];
for(int j=m;j>=1;j--)
RH[j]=RH[j+1]*Base+s[i][j];
int lst=1;
for(int j=1;j<=m;j++)
{
if(s[i][j]=='_'||j==m)
{
int l=lst,r=j-1;
lst=j+1;
if(s[i][j]!='_') r++;
if(l>r) continue;
ull hsh=H[r]-H[l-1]*pw[r-l+1];
ull rhsh=RH[l]-RH[r+1]*pw[r-l+1];
if(hsh==rhsh)
{
if(mp2[hsh]) continue;
ans++,mp2[hsh]=1;
continue;
}
if(!mp1[hsh])
{
int turn=1;
for(int k=1;k<=r-l+1;k++)
{
if(s[i][l+k-1]>s[i][r-k+1]) break;
else if(s[i][l+k-1]<s[i][r-k+1])
{
turn=-1;
break;
}
}
mp1[hsh]=++id_tot,big[id_tot]=turn;
mp1[rhsh]=++id_tot,big[id_tot]=-turn;
}
rtot[i]++;
row[0][i][rtot[i]]=mp1[hsh];
row[1][i][rtot[i]]=mp1[rhsh];
}
}
}
for(int j=1;j<=m;j++)
{
for(int i=1;i<=n;i++)
H[i]=H[i-1]*Base+s[i][j];
for(int i=n;i>=1;i--)
RH[i]=RH[i+1]*Base+s[i][j];
int lst=1;
for(int i=1;i<=n;i++)
{
if(s[i][j]=='_'||i==n)
{
int l=lst,r=i-1;
lst=i+1;
if(s[i][j]!='_') r++;
if(l>r) continue;
ull hsh=H[r]-H[l-1]*pw[r-l+1];
ull rhsh=RH[l]-RH[r+1]*pw[r-l+1];
if(hsh==rhsh)
{
if(mp2[hsh]) continue;
ans++,mp2[hsh]=1;
continue;
}
if(!mp1[hsh])
{
int turn=1;
for(int k=1;k<=r-l+1;k++)
{
if(s[l+k-1][j]>s[r-k+1][j]) break;
else if(s[l+k-1][j]<s[r-k+1][j])
{
turn=-1;
break;
}
}
mp1[hsh]=++id_tot,big[id_tot]=turn;
mp1[rhsh]=++id_tot,big[id_tot]=-turn;
}
ctot[j]++;
col[0][j][ctot[j]]=mp1[hsh];
col[1][j][ctot[j]]=mp1[rhsh];
}
}
}
int S=id_tot+n+m+1,T=S+1;
for(int i=1;i<=id_tot;i+=2)
{
int x=i,y=i+1;
if(big[x]==1) swap(x,y);
add_edge(x,y,2);
}
for(int i=1;i<=n;i++)
{
if(!rtot[i]) continue;
if((a[i]==1&&big[row[0][i][1]]<0)||(a[i]==-1&&big[row[1][i][1]]<0))
{
add_edge(S,id_tot+i,INF);
for(int j=1;j<=rtot[i];j++)
{
if(a[i]==1) add_edge(id_tot+i,row[0][i][j],INF);
else add_edge(id_tot+i,row[1][i][j],INF);
}
}
else if((a[i]==1&&big[row[0][i][1]]>0)||(a[i]==-1&&big[row[1][i][1]]>0))
{
add_edge(id_tot+i,T,INF);
for(int j=1;j<=rtot[i];j++)
{
if(a[i]==1) add_edge(row[0][i][j],id_tot+i,INF);
else add_edge(row[1][i][j],id_tot+i,INF);
}
}
else
{
for(int j=1;j<=rtot[i];j++)
{
int x=row[0][i][j],y=row[1][i][j];
if(big[x]>0) swap(x,y);
add_edge(id_tot+i,x,INF);
add_edge(y,id_tot+i,INF);
}
}
}
for(int i=1;i<=m;i++)
{
if(!ctot[i]) continue;
if((b[i]==1&&big[col[0][i][1]]<=0)||(b[i]==-1&&big[col[1][i][1]]<=0))
{
add_edge(S,id_tot+n+i,INF);
for(int j=1;j<=ctot[i];j++)
{
if(b[i]==1) add_edge(id_tot+n+i,col[0][i][j],INF);
else add_edge(id_tot+n+i,col[1][i][j],INF);
}
}
else if((b[i]==1&&big[col[0][i][1]]>=0)||(b[i]==-1&&big[col[1][i][1]]>=0))
{
add_edge(id_tot+n+i,T,INF);
for(int j=1;j<=ctot[i];j++)
{
if(b[i]==1) add_edge(col[0][i][j],id_tot+n+i,INF);
else add_edge(col[1][i][j],id_tot+n+i,INF);
}
}
else
{
for(int j=1;j<=ctot[i];j++)
{
int x=col[0][i][j],y=col[1][i][j];
if(big[x]>0) swap(x,y);
add_edge(id_tot+n+i,x,INF);
add_edge(y,id_tot+n+i,INF);
}
}
}
print(ans+dinic(S,T),'\n');
}
int main(){
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
int T;
read(T);
pw[0]=1;
for(int i=1;i<=75;i++)
pw[i]=pw[i-1]*Base;
while(T--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 10
Accepted
time: 0ms
memory: 3556kb
input:
64 9 9 0 0 0 0 1 0 0 0 0 0 0 0 0 0 -1 0 1 -1 BB_AC_BC_ BC_CC_CC_ _________ AB_AB_AC_ BC_BB_BC_ _________ AC_BC_BC_ CC_BC_BC_ _________ 9 9 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 AB_AA_AB_ BB_AA_BC_ _________ AB_AB_AC_ AB_BC_BC_ _________ AA_AB_AC_ AB_BB_AC_ _________ 9 9 0 0 0 0 -1 0 0 0 0 0 -1 0 0 0 ...
output:
2 3 3 3 7 2 7 5 3 7 7 3 5 2 5 3 4 5 7 3 9 3 9 3 7 9 3 3 3 2 3 9 3 3 3 5 4 9 5 3 3 3 7 5 7 2 9 3 3 7 7 7 5 2 5 3 3 5 3 3 3 5 3 5
result:
ok 64 lines
Test #2:
score: 10
Accepted
time: 2ms
memory: 3704kb
input:
64 9 9 0 0 0 0 1 1 1 0 0 0 0 0 0 -1 0 0 0 -1 BD_AD_BC_ CD_DD_CD_ _________ AD_BD_BC_ BD_CD_BC_ _________ AD_AC_AC_ DD_AD_AC_ _________ 9 9 0 0 -1 0 -1 0 0 0 -1 1 0 0 -1 0 -1 0 0 0 BC_BC_AC_ CD_CD_BD_ _________ CD_BC_BC_ DD_BC_CD_ _________ AD_AC_BB_ DD_CD_BD_ _________ 9 9 0 0 0 1 0 0 0 0 0 0 1 0 -1...
output:
6 9 5 7 3 4 4 7 3 8 6 8 5 5 7 3 7 8 11 6 4 8 3 6 4 6 6 3 4 3 4 4 7 3 8 4 8 4 3 4 8 4 6 4 12 8 3 4 7 8 3 3 3 14 3 14 7 8 4 4 10 4 4 8
result:
ok 64 lines
Test #3:
score: 10
Accepted
time: 4ms
memory: 3452kb
input:
64 18 18 0 0 0 0 0 -1 0 0 1 0 0 0 0 0 0 0 0 0 0 -1 1 0 0 0 0 -1 0 1 0 0 0 1 0 0 0 1 AC_BD_FG_BE_AA_AG_ BE_CE_FG_CF_AD_GG_ __________________ BD_CC_BE_BE_BE_BE_ CF_CE_DE_CG_BG_CF_ __________________ BD_AE_AE_AC_CE_AF_ CG_EF_EG_CD_CF_BF_ __________________ CF_DE_BE_AB_AD_CF_ FF_DG_BG_BD_AF_EF_ _______...
output:
19 21 10 32 33 13 20 15 15 15 6 18 11 7 21 36 13 28 26 7 7 15 21 23 29 23 17 15 35 13 15 31 13 17 30 16 27 6 13 15 36 15 29 34 7 22 17 30 28 36 7 16 21 35 28 23 29 6 15 7 12 32 6 16
result:
ok 64 lines
Test #4:
score: 10
Accepted
time: 4ms
memory: 3496kb
input:
64 18 18 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 AG_EF_AF_AD_BE_BC_ BH_EF_FG_CH_CF_CE_ __________________ BE_AC_BC_EF_DD_AE_ DH_BH_BG_EH_DF_CH_ __________________ AE_AB_BB_AF_BE_EG_ DG_AE_BB_AF_EG_EH_ __________________ DE_AD_CD_BF_BG_AG_ EG_DE_CH_CG_EG_CG_ _________...
output:
16 13 20 11 15 31 14 16 8 41 16 15 30 21 43 35 27 8 26 21 23 38 33 23 17 5 17 16 29 6 7 5 15 38 17 16 34 16 11 37 26 16 16 23 20 6 30 7 35 7 10 6 33 18 42 39 13 22 23 7 7 41 6 14
result:
ok 64 lines
Test #5:
score: 10
Accepted
time: 4ms
memory: 3588kb
input:
64 24 24 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 ABC_AAC_ABC_AAB_AAB_AAB_ ACD_ABD_ABD_ABC_ABD_ABC_ CDD_BDD_BDD_ACD_ACD_ACD_ ________________________ ABC_ABD_ABB_ABC_AAB_ACD_ BCD_ACD_ABD_ACD_ABD_BDD_ CCD_BDD_BCD_CCD_ABD_CDD_ ___________________...
output:
7 3 32 28 18 9 4 12 3 11 18 16 28 23 25 20 24 10 3 11 19 10 18 22 10 8 12 4 16 16 26 12 18 25 13 21 19 23 18 24 12 4 12 16 22 30 4 10 15 24 16 30 25 25 3 15 20 16 4 19 22 12 4 14
result:
ok 64 lines
Test #6:
score: 10
Accepted
time: 5ms
memory: 3532kb
input:
64 24 24 -1 0 0 0 0 0 1 0 1 0 -1 0 -1 0 0 0 0 -1 0 1 0 1 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 1 1 -1 0 0 AAC_AAB_ABB_BBC_ABC_ACD_ ACD_ABC_ABE_BBE_BBE_CDD_ BDD_BBD_BBE_BDE_BCE_CDE_ ________________________ ACD_ABC_ABD_ABC_ABD_ABB_ BDE_ACE_ACD_ABE_ADE_ABC_ CEE_CDE_CDD_BEE_CDE_BCC_ _____________...
output:
31 30 12 22 25 37 34 11 3 25 16 16 4 12 45 12 13 15 22 5 20 34 26 30 26 34 11 10 21 24 20 11 29 37 27 3 18 11 20 13 20 39 4 3 10 25 21 24 11 43 23 16 27 23 15 36 13 21 25 11 9 33 36 10
result:
ok 64 lines
Test #7:
score: 10
Accepted
time: 45ms
memory: 3904kb
input:
64 72 72 -1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 -1 0 0 -1 0 1 0 0 0 0 0 0 0 -1 0 0 0 1 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 -1 0 0 -1 1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 0 1 -1 0 0 0...
output:
182 350 392 176 293 247 106 224 98 248 96 297 350 224 220 258 266 258 150 280 63 247 364 138 375 229 198 136 320 108 210 284 254 256 164 296 326 321 198 272 292 318 98 346 314 273 266 133 194 286 340 264 330 204 385 406 326 267 265 208 256 205 144 226
result:
ok 64 lines
Test #8:
score: 10
Accepted
time: 11ms
memory: 3600kb
input:
64 72 72 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 -1 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 -1 0 0 ...
output:
47 57 37 53 41 55 61 49 59 49 51 63 55 17 59 55 47 49 39 59 57 61 45 61 59 43 59 55 49 57 57 51 41 59 59 59 23 57 63 57 47 57 53 55 61 37 61 57 53 49 61 59 55 59 55 59 61 51 61 53 61 63 55 57
result:
ok 64 lines
Test #9:
score: 10
Accepted
time: 18ms
memory: 3656kb
input:
64 72 72 0 0 -1 0 0 0 1 0 0 0 0 0 0 -1 0 0 0 1 -1 0 0 0 0 0 1 0 0 0 -1 0 0 -1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 -1 0 0 0 1 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 -1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 -1 0 -1 0 0 0 0 0 1 0 0 0 -1 0 -1 0 0 0 0 0 0 0...
output:
88 78 78 96 72 62 82 84 96 78 86 90 65 76 84 66 96 60 82 62 84 66 90 78 84 88 74 74 98 88 58 90 93 72 74 58 44 90 60 94 86 82 96 68 88 69 90 66 82 98 61 62 76 58 94 56 78 66 60 58 76 76 70 78
result:
ok 64 lines
Test #10:
score: 10
Accepted
time: 13ms
memory: 3628kb
input:
64 70 70 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 -1 0 0 0 0 0 0 0 -1 0 0 0 0 -1 0 -1 -1 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 1 0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 -1 0 0 0 0 0 0 0 0 1 0 0 -1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 0 0 0 0 0 ...
output:
25 27 27 23 21 27 25 25 23 21 23 23 25 25 25 17 27 21 21 17 23 23 25 23 23 23 23 19 25 25 27 25 15 25 25 27 17 25 21 23 25 25 25 23 23 23 27 25 25 21 27 23 25 25 21 19 25 19 21 27 23 21 27 25
result:
ok 64 lines