博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #460 Div. 2 C.D
阅读量:4878 次
发布时间:2019-06-11

本文共 2839 字,大约阅读时间需要 9 分钟。

C……没啥好说的……感觉……找一行或一列中有多少个连续的k个‘.’(虽然因为一个i,j写反了搞了好久才改出来orz)

(k==1的时候要特判一下,或者直接ans/2也可以的样子)

#include
using namespace std;const int maxn=1e5;int n,m,k,ans=0;char mz[2005][2005];int main(){ cin>>n>>m>>k; for(int i=0;i
>mz[i][j]; if(k==1) { for(int i=0;i
m&&k>n) ans=0; else { for(int i=0;i
=k) ans++; } else cnt=0; } } for(int i=0;i
=k) ans++; } else cnt=0; } } } cout<
<

D.

DP+拓扑排序

It is obvious that we can use dynamic programming algorithm to solve this stupid problem.

We can make an array f[i][j] to respect when you are at the point i, then how many letters j you can get. Note that i is ranging from 1 to n and j is from 1 to 26.

Then, you can do this dynamic programming algorithm by topological sorting. Specifically, you can use deg[i] to show how many edges that end at point i. First put all points i which satisfy deg[i] = 0 into a queue. When the queue is not empty, do the following steps repeatedly.

  1. Take one point from the front of the queue. We say it is i.
  2. Iterate all edges which begin at the point i. We say the endpoint of the edge is k. Then we update f[k][·] using f[i][·]. Finally we make deg[k] minus by 1. If deg[k] = 0, then we add the point k into the queue.
  3. Make a variable cnt plus by 1.

When there is no cycle in the graph, the queue will be empty with cnt = n. Then the answer is the maximum number among f[i][j]. If there are cycles in the graph, then cnt cannot be n. Thus the answer can be arbitrarily large. In that situation, we should output -1.

Time complexity: , where alpha is the size of the alphabet (i.e. alpha = 26).

Memory complexity: .

#include
using namespace std;typedef long long ll;const int maxn=3e5+5;int n,m,cnt=0;int dp[maxn][26],deg[maxn],head[maxn];string a;queue
q;struct edge{int to,nxt;}e[maxn<<1];void add(int u,int v){ e[cnt].to=v; e[cnt].nxt=head[u]; head[u]=cnt++;}void topo(){ queue
q; for(int i=1;i<=n;i++) { if(!deg[i]) { q.push(i); dp[i][a[i-1]-'a']++; } } int crt=0; while(!q.empty()) { int u = q.front();q.pop(); crt++; for(int i=head[u];~i;i=e[i].nxt) { int v=e[i].to; deg[v]--; if(!deg[v]) q.push(v); for(int j=0;j<26;j++) dp[v][j]=max(dp[v][j],dp[u][j]+(a[v-1]-'a'==j)); } } if(crt
>n>>m>>a; memset(head,-1,sizeof(head)); memset(dp,0,sizeof(0)); for(int i=0;i
>u>>v; add(u,v); deg[v]++; } topo(); return 0;}

 

转载于:https://www.cnblogs.com/Egoist-/p/8397930.html

你可能感兴趣的文章
codeblocks如何支持_tmain?可移植代码的编码推荐
查看>>
省市联动 填坑
查看>>
canvas写的一个小时钟demo
查看>>
原来今天是冬至
查看>>
又混了一天班
查看>>
九度oj 1006
查看>>
HDU6400-2018ACM暑假多校联合训练1004-Parentheses Matrix-构造
查看>>
最短路问题专题
查看>>
《Redis复制与可扩展集群搭建》看后感
查看>>
Jquery Mobile总结
查看>>
223. Rectangle Area
查看>>
spring boot + velocity中文乱码解决方式
查看>>
读罢泪两行,人生成长必须面对的10个残酷事实
查看>>
ASP 32位程序运行与64位问题:ADODB.Connection 错误 '800a0ea9' 未指定提供程序,也没有指派的默认提供程序。...
查看>>
xcode-git笔记
查看>>
TCP和UDP的优缺点及区别
查看>>
MATLAB消除曲线毛刺Outlier Detection and Removal [hampel]
查看>>
MySQL DATE_SUB() 函数
查看>>
在SSH框架下按条件分页查询
查看>>
jquery选择器
查看>>