A. One-dimensional Japanese Crossword
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized a × b squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents how many groups of black squares there are in corresponding row or column, and the integers themselves represents the number of consecutive black squares in corresponding group (you can find more detailed explanation in Wikipedia https://en.wikipedia.org/wiki/Japanese_crossword).

Adaltik decided that the general case of japanese crossword is too complicated and drew a row consisting of n squares (e.g. japanese crossword sized 1 × n), which he wants to encrypt in the same way as in japanese crossword.

The example of encrypting of a single row of japanese crossword.

Help Adaltik find the numbers encrypting the row he drew.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 100) — the length of the row. The second line of the input contains a single string consisting of n characters 'B' or 'W', ('B' corresponds to black square, 'W' — to white square in the row that Adaltik drew).

Output

The first line should contain a single integer k — the number of integers encrypting the row, e.g. the number of groups of black squares in the row.

The second line should contain k integers, encrypting the row, e.g. corresponding to sizes of groups of consecutive black squares in the order from left to right.

Examples
input
3BBW

output
12 

input
5BWBWB

output
31 1 1 

input
4WWWW

output
0

input
4BBBB

output
14 

input
13WBBBBWWBWBBBW

output
34 1 3 

Note

The last sample case correspond to the picture in the statement.

题意:输出连续B的个数;

思路:模拟;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+10,M=1e6+10,inf=1e9+10,mod=1e9+7;
char a[N];
vector<int>ans;
int main()
{int n;scanf("%d",&n);scanf("%s",a);for(int i=0;i<n;i++){int sum=0;if(a[i]=='W')continue;while(a[i]=='B'){sum++;i++;}ans.push_back(sum);}cout<<ans.size()<<endl;for(int i=0;i<ans.size();i++)cout<<ans[i]<<" ";return 0;
}

B. Passwords
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.

Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.

Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.

Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).

Input

The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.

The next n lines contains passwords, one per line — pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters.

The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords.

Output

Print two integers — time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively.

Examples
input
5 2cbaabcbb1abCABCabc

output
1 15

input
4 10011221222

output
3 4

Note

Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.

Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all.

题意:给你一个n个字符串,从长度小的到大的输入,每k次会等待五秒,求最坏和最好的情况;

思路:模拟;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+10,M=1e6+10,inf=1e9+10,mod=1e9+7;
char ch[110][110];
char pass[N];
int main()
{int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)scanf("%s",&ch[i]);int flag=0,k=0;scanf("%s",pass);for(int i=1;i<=n;i++){if(strlen(ch[i])<strlen(pass))flag++;if(strcmp(ch[i],pass)==0)k++;if(strlen(ch[i])>strlen(pass))k++;}n=n-k+1;int ans=0;while(n){ans+=min(n,m);n-=min(n,m);if(n==0)break;ans+=5;}printf("%d %d\n",flag+1+(flag/m)*5,ans);return 0;
}

C. Journey
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from1 to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are nocyclic routes between showplaces.

Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units.

Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than Ttime units passing it.

Input

The first line of the input contains three integers n, m and T (2 ≤ n ≤ 5000,  1 ≤ m ≤ 5000,  1 ≤ T ≤ 109) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively.

The next m lines describes roads in Berlatov. i-th of them contains 3 integers ui, vi, ti (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ ti ≤ 109), meaning that there is a road starting from showplace ui and leading to showplace vi, and Irina spends ti time units to pass it. It is guaranteed that the roads do not form cyclic routes.

It is guaranteed, that there is at most one road between each pair of showplaces.

Output

Print the single integer k (2 ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line.

Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.

If there are multiple answers, print any of them.

Examples
input
4 3 131 2 52 3 72 4 8

output
31 2 4 

input
6 6 71 2 21 3 33 6 32 4 24 6 26 5 1

output
41 2 4 6 

input
5 5 61 3 33 5 31 2 22 4 34 5 2

output
31 3 5 

题意:给你一个图,n个点,m条边,总时间T;求在T时间内能走过最多的点从1-n;

思路:拓扑dp;dp[i][j]表示从1-i经过j个点花费的时间;

   坑点:有无关的点;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
const int N=2e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
const ll INF=1e18+10;
int dp[5005][5005];
int du[N];
struct is
{int v,w;int next;
}edge1[N],edge2[N] ;
int jiedge1,jiedge2;
int head1[N],head2[N];
int flag[N];
int n,m,T;
void init()
{memset(du,0,sizeof(du));memset(head1,0,sizeof(head1));memset(head2,0,sizeof(head2));memset(flag,0,sizeof(flag));jiedge1=0;jiedge2=0;
}
void add1(int u,int v,int w)
{++jiedge1;edge1[jiedge1].v=v;edge1[jiedge1].w=w;edge1[jiedge1].next=head1[u];head1[u]=jiedge1;
}
void add2(int u,int v,int w)
{++jiedge2;edge2[jiedge2].v=v;edge2[jiedge2].w=w;edge2[jiedge2].next=head2[u];head2[u]=jiedge2;
}
void bfs(int u)
{queue<int>q;q.push(u);dp[1][1]=0;while(!q.empty()){int u=q.front();q.pop();for(int i=head1[u];i;i=edge1[i].next){int v=edge1[i].v;int w=edge1[i].w;for(int t=1;t<=n;t++){if(dp[u][t-1]+w<=T){dp[v][t]=min(dp[v][t],dp[u][t-1]+w);}}if(--du[v]==0&&!flag[v])q.push(v);}}
}
int ans[N];
void getans(int u,int x)
{printf("%d\n",x);int flag=x;while(u!=1){for(int i=head2[u];i;i=edge2[i].next){int v=edge2[i].v;int w=edge2[i].w;if(w+dp[v][x-1]==dp[u][x]){ans[x]=v;x--;u=v;break;}}}for(int i=2;i<=flag;i++)printf("%d ",ans[i]);printf("%d\n",n);
}
int main()
{scanf("%d%d%d",&n,&m,&T);for(int i=0;i<m;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);add1(u,v,w);add2(v,u,w);du[v]++;}for(int i=0;i<=n;i++)for(int t=0;t<=n;t++)dp[i][t]=inf;queue<int>q;for(int i=2;i<=n;i++)if(du[i]==0)q.push(i),flag[i]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head1[u];i;i=edge1[i].next){int v=edge1[i].v;du[v]--;if(du[v]==0&&v!=1)q.push(v),flag[v]=1;}}bfs(1);for(int i=n;i>=0;i--)if(dp[n][i]<inf){getans(n,i);break;}return 0;
}

转载于:https://www.cnblogs.com/jhz033/p/5927437.html

Codeforces Round #374 (Div. 2) A , B , C 水,水,拓扑dp相关推荐

  1. Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crosswor 水题

    A. One-dimensional Japanese Crossword 题目连接: http://codeforces.com/contest/721/problem/A Description ...

  2. Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

    Codeforces Round #459 (Div. 2) C. The Monster 题意:定义正确的括号串,是能够全部匹配的左右括号串. 给出一个字符串,有 (.). ? 三种字符, ? 可以 ...

  3. Codeforces Round #731 (Div. 3) G. How Many Paths? dfs + 拓扑 + 思维

    传送门 题意: 给你一张nnn个点mmm条边的图,让你对每个点确定一个编号,规则如下: (1)(1)(1) 对于不能到的点编号为000. (2)(2)(2) 对于只有一条路径能到这个点的点编号为111 ...

  4. Codeforces Round #579 (Div. 3) F2. Complete the Projects (hard version) dp + 贪心

    传送门 文章目录 题意: 思路: 题意: 思路: 排序方式跟easyeasyeasy版本的一样,但是hardhardhard版本是输出最多能选多少,所以我们对b<0b<0b<0的情况 ...

  5. Codeforces Round #590 (Div. 3) F. Yet Another Substring Reverse 子集dp

    传送门 文章目录 题意: 思路: 题意: 思路: 之前做过类似的题,翻转一个字串相当于将任意两个不相交的串连在一起.再一看字符集≤20\le20≤20,那就是铁子集dpdpdp了. 定义f[i]f[i ...

  6. Codeforces Round #598 (Div. 3) E. Yet Another Division Into Teams dp + 输出方案

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的序列aaa,你需要将其分成若干组,每组的价值为max⁡(ai)−min(ai)\max(a_i)-min(a_i)max(ai​)−mi ...

  7. Codeforces Round #599 (Div. 2) E. Sum Balance 图转换 + 子集dp + 环

    传送门 文章目录 题意: 思路: 题意: 思路: 首先我们知道如果所有数的和summodk!=0sum\bmod k!=0summodk!=0那么此时无解,否则我们设need=sum/kneed=su ...

  8. Codeforces Round #635 (Div. 1) C. Kaavi and Magic Spell 区间dp

    传送门 文章目录 题意: 思路: 题意: 给你两个串s,ts,ts,t,每次都可以从sss的开头拿一个字符放到AAA串的开头或结尾,问最终有多少种方案使得ttt是AAA的前缀,注意sss不必全部拿完. ...

  9. Codeforces Round #674 (Div. 3) F. Number of Subsequences 简单计数dp

    传送门 文章目录 题意: 思路: 题意: 给你一个长度为nnn的串,包含a,b,c,?a,b,c,?a,b,c,?四种字符,其中???可以变成为a,b,ca,b,ca,b,c的任意一种,让你求abca ...

  10. Codeforces Round #622 (Div. 2) D. Happy New Year 状压dp

    传送门 文章目录 题意: 思路: 题意: n≤1e5,m≤1e9,k≤8.n\le 1e5,m\le 1e9,k\le 8.n≤1e5,m≤1e9,k≤8. 思路: 注意到题目中保证了每个孩子至多收到 ...

最新文章

  1. Python代码选中文乱码
  2. 如何使double为整数时不带小数点
  3. java中web service的几种实现方式(自用)
  4. Validation failed for one or more entities. See ‘EntityValidationErrors’解决方法
  5. python学习6 web开发
  6. 支付宝小程序中“”号写法
  7. sqlite 中出现的database table is locked 解决办法
  8. Win10下Eclipse运行环境的安装
  9. Axure9学习笔记1:介绍及安装
  10. linux 16.04系统下载,【ubuntu16.04】ubuntu(乌班图系统)镜像文件下载 v16.04 稳定版本-七喜软件园...
  11. H.264 学习建议
  12. 用wget命令从FTP服务器下载数据
  13. 数控车床 刀尖补偿用法 G41 G42 G40
  14. Conduit-面向Kubernetes的轻量化服务网格
  15. python读取文本两个数字的成语_只要2步!将搜狗词库(scel)转为Python可读的文本...
  16. 获取系统中已经安装的文字输入法
  17. python实现自然语言处理之文本分词
  18. Qt 信号槽的应用(一)
  19. cmd的发送 mmc_mmc/sd 卡介绍
  20. 10-89 将学号为“1911201”的学生系别改为“经管学院”,班级改为“19经管1”(update的用法)

热门文章

  1. python算法书推荐-你也能看得懂的Python算法书
  2. JZOJ 4675. 【NOIP2016提高A组模拟7.21】Double-row
  3. cad能整体比例缩小吗_CAD中两个缩放的不同及其各自的应用
  4. word中如何隐藏表格线框
  5. BZOJ 2456 mode
  6. POJ 1703 Find them, Catch them(并查集高级应用)
  7. mysql行级安全_MySQL学习笔记(五):MySQL表级锁和行级锁
  8. 2021-03-09 Local Lipschitz 可能存在 有限时间逃逸
  9. 【52】写了placement new也要写placement delete
  10. 《改善java代码》第一章:java开发通用原则