Codeforces 1099 C. Postcard-字符串处理(Codeforces Round #530 (Div. 2))
1 second
256 megabytes
standard input
standard output
Andrey received a postcard from Irina. It contained only the words "Hello, Andrey!", and a strange string consisting of lowercase Latin letters, snowflakes and candy canes. Andrey thought that this string is an encrypted message, and decided to decrypt it.
Andrey noticed that snowflakes and candy canes always stand after the letters, so he supposed that the message was encrypted as follows. Candy cane means that the letter before it can be removed, or can be left. A snowflake means that the letter before it can be removed, left, or repeated several times.
For example, consider the following string:

This string can encode the message «happynewyear». For this, candy canes and snowflakes should be used as follows:
- candy cane 1: remove the letter w,
- snowflake 1: repeat the letter p twice,
- candy cane 2: leave the letter n,
- snowflake 2: remove the letter w,
- snowflake 3: leave the letter e.

Please note that the same string can encode different messages. For example, the string above can encode «hayewyar», «happpppynewwwwwyear», and other messages.
Andrey knows that messages from Irina usually have a length of kk letters. Help him to find out if a given string can encode a message of kkletters, and if so, give an example of such a message.
The first line contains the string received in the postcard. The string consists only of lowercase Latin letters, as well as the characters «*» and «?», meaning snowflake and candy cone, respectively. These characters can only appear immediately after the letter. The length of the string does not exceed 200200.
The second line contains an integer number kk (1≤k≤2001≤k≤200), the required message length.
Print any message of length kk that the given string can encode, or «Impossible» if such a message does not exist.
hw?ap*yn?eww*ye*ar
12
happynewyear
ab?a
2
aa
ab?a
3
aba
ababb
5
ababb
ab?a
1
Impossible
题意就是给你一个字符串,其中?可以有两种操作,1.将前面的字母放到当前位置,其实就是去掉?2.去掉前面的字母,其实就是去掉前面的字母去掉?
*的有三种操作,1.将前面的字母放到当前位置,就是去掉* 2.去掉前面的字母,就是去掉前面的字母和* 3.前面的字母可以重复任意次
假设是abc*,可以是abc,可以是ab,可以是abcccccc,就是这样的,给你一个字符串,问你能不能将字符串变成要求的长度,最后的字符串不能有?和*。
直接将纯字母的长度先找出来,然后判断再进行操作就可以了。
代码:
//C
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn=1e5+; char s[]; int main()
{
scanf("%s",s);
int n;
cin>>n;
int len=strlen(s);
int candy=,snow=;
for(int i=;i<len;i++){
if(s[i]=='?') candy++;
else if(s[i]=='*') snow++;
}
int l=len-candy-snow;
if(l==n){
for(int i=;i<len;i++){
if(s[i]!='?'&&s[i]!='*') cout<<s[i];
}
cout<<endl;
}
else if(l<n&&snow>){
int c;
for(int i=;i<len;i++){
if(s[i]=='?') s[i]='#';
if(s[i]=='*'){
if(l<n){
c=n-l;
s[i]='!';
//cout<<s[i]<<endl;
l=n;
}
else{
s[i]='#';
}
}
}
for(int i=;i<len;i++){
if(s[i]!='#'){
if(s[i]=='!'){
for(int j=;j<c;j++)
cout<<s[i-];
}
else cout<<s[i];
}
}
cout<<endl;
}
else if(l>n&&l-(candy+snow)<=n){
for(int i=;i<len;i++){
if((s[i]=='?'||s[i]=='*')&&l>n){
s[i-]='#';
s[i]='#';
l--;
}
else if(s[i]=='?'||s[i]=='*')
s[i]='#';
}
for(int i=;i<len;i++){
if(s[i]!='#') cout<<s[i];
}
cout<<endl;
}
else cout<<"Impossible"<<endl;
}
Codeforces 1099 C. Postcard-字符串处理(Codeforces Round #530 (Div. 2))的更多相关文章
- Codeforces 1099 D. Sum in the tree-构造最小点权和有根树 贪心+DFS(Codeforces Round #530 (Div. 2))
D. Sum in the tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 1099 B. Squares and Segments-思维(Codeforces Round #530 (Div. 2))
B. Squares and Segments time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Codeforces 1099 A. Snowball-暴力(Codeforces Round #530 (Div. 2))
A. Snowball time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #530 (Div. 2) A,B,C,D
A. Snowball 链接:http://codeforces.com/contest/1099/problem/A 思路:模拟 代码: #include<bits/stdc++.h> ...
- Codeforces Round #530 (Div. 2) F (树形dp+线段树)
F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...
- Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)
D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...
- Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)
https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- Codeforces Round #530 (Div. 2) Solution
A. Snowball 签. #include <bits/stdc++.h> using namespace std; ], d[]; int main() { while (scanf ...
随机推荐
- BIRT-商务智能报表工具开发案例指南
BIRT 报表 脚本 开发 数据库 http://www.iteye.com/topic/1128694 打算近期出版一本全面介绍BIRT使用的书籍,能够帮助大家全面了解BIRT的方方面面,用丰富的案 ...
- 「模板」「讲解」Treap名次树
Treap实现名次树 前言 学平衡树的过程可以说是相当艰难.浏览Blog的过程中看到大量指针版平衡树,不擅长指针操作的我已经接近崩溃.于是,我想着一定要写一篇非指针实现的Treap的Blog. 具体如 ...
- Item 30 用enum代替int常量类型枚举,string常量类型枚举
1.用枚举类型替代int枚举类型和string枚举类型 public class Show { // Int枚举类型 // public static final int APPLE_FUJI ...
- 【BZOJ1975】【SDOI2010】魔法猪学院 [A*搜索]
魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description iPig在假期来到了传说中的魔法猪 ...
- bzoj 1635: [Usaco2007 Jan]Tallest Cow 最高的牛——差分
Description FJ's N (1 <= N <= 10,000) cows conveniently indexed 1..N are standing in a line. E ...
- 【Zigbee技术入门教程-02】一图读懂ZStack协议栈的核心思想与工作机理
[Zigbee技术入门教程-02]一图读懂ZStack协议栈的核心思想与工作机理 广东职业技术学院 欧浩源 Z-Stack协议栈是一个基于任务轮询方式的操作系统,其任务调度和资源分配由操作系统抽 ...
- web服务器和数据库服务器不在一台机器上
把localhost改成数据库所在的IP就行了. $link=mysql_connect( "202.195.246.202 ", "root ", " ...
- 网络设备之net_device结构与操作
net_device结构是一个很大的结构,其中包含了硬件信息,接口信息,其他辅助信息,以及设备操作函数等: 目前仍在读代码中,后续字段注释会逐渐补充: /** * struct net_device ...
- Linux内核通知链分析【转】
转自:http://www.cnblogs.com/jason-lu/articles/2807758.html Linux内核通知链分析 1. 引言 Linux是单内核架构(monolithic k ...
- python mysql插入数据遇到的错误
1.数据插入的时候报错:not enough arguments for format string,大概意思就是说没有足够的参数格式化字符串. 我的数据库插入方法是这样的 def add_data( ...