CodeForces 374(div2)

本文介绍了一维日本填字游戏的算法实现,通过解析字符串来生成填字游戏的答案线索。此外,还探讨了按长度排序尝试密码的最佳与最坏情况时间分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C题是个DP啊,可怜我昨晚在那苦苦暴力了,课上刚学的DP,真是对不住算法课老师。。

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
3
BBW
output
1
2 
input
5
BWBWB
output
3
1 1 1 
input
4
WWWW
output
0
input
4
BBBB
output
1
4 
input
13
WBBBBWWBWBBBW
output
3
4 1 3 
Note

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

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> 
#include <cmath>
using namespace std;

typedef long long int ll;

int sum=0;
int arr[100];
int Count=0;
int main(){
	
	char a[105];
	int N;
	scanf("%d",&N);
	scanf("\n%s",a);
	for(int i=0; i<N; i++){
		if(a[i]=='B' && (a[i+1]=='W'|| a[i+1]=='\0')){
			sum++;
			arr[Count++] = sum;
			sum = 0; 
		}
		else if(a[i]=='B' && a[i+1]=='B')
			sum++;
	}
	cout << Count << endl;
	if(Count!=0)
		cout << arr[0];
	for(int i=1; i<Count; i++)
		cout << " " << arr[i];
	cout << endl; 
	return 0;
} 


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 2
cba
abc
bb1
abC
ABC
abc
output
1 15
input
4 100
11
22
1
2
22
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 5 seconds 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.


#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long int ll;

char s[105][105];
char pass[105];
int correct=0;
int a[105];

int main(){
	
	int N,K;
	scanf("%d %d",&N,&K);
	int len;
	for(int i=1; i<=N; i++)
		scanf("\n%s",s[i]);
	scanf("\n%s",pass);
	for(int i=1; i<=N; i++){
		len = strlen(s[i]);
		a[len]++;
		if(strcmp(pass,s[i])==0)
			correct++;
	}
	len = strlen(pass);
	int Min,Max;
	int sum=0;
	for(int i=1; i<len; i++)
		sum += a[i];
	
	if((sum+1)%K==0)
		Min = ((sum+1)/K)*5 + sum+1 -5;
	else
		Min = ((sum+1)/K)*5 + sum+1;
	if((sum+a[len]-correct+1)%K==0)
		Max = ((sum+a[len]-correct+1)/K)*5 + sum+a[len]-correct+1 -5;
	else
		Max = ((sum+a[len]-correct+1)/K)*5 + sum+a[len]-correct+1;
	cout << Min << endl;
	cout << Max << endl;
	
	return 0;
} 


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 exceedingT. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than T time 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 13
1 2 5
2 3 7
2 4 8
output
3
1 2 4 
input
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
output
4
1 2 4 6 
input
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
output
3
1 3 5 

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <stack>
using namespace std;

const int maxn = 5005;
const int inf = 0x3f3f3f3f;
struct Node{
	int x;
	int y;
	int val;
};
Node a[maxn];
int DP[maxn][maxn];
int road[maxn][maxn];
stack<int> st;

int main(){

	int N,M,K;
	scanf("%d %d %d",&N,&M,&K);
	for(int i=0; i<M; i++)
		scanf("%d %d %d",&a[i].x,&a[i].y,&a[i].val);
	memset(DP,0x3f,sizeof(DP));
	memset(road,0,sizeof(road));
	int pos;
	int u,v,w;
	DP[1][1] = 0;//很重要的初始化
	for(int i=2; i<=N; i++){
		for(int j=0; j<M; j++){
			u = a[j].x;
			v = a[j].y;
			w = a[j].val;
			if((DP[i-1][u]+w) < DP[i][v]){
				DP[i][v] = DP[i-1][u]+w;
				road[i][v] = u;
			}
		}
		if(DP[i][N] <= K)
			pos = i;
	}
	cout << pos << endl;

	int cur = N;
	while(cur){
		st.push(cur);
		cur = road[pos--][cur];
	}

	int flag=0;
	while(!st.empty()){
		if(flag++)
			cout << " " << st.top();
		else
			cout << st.top();
		st.pop();
	}
	cout << endl;

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值