Codeforces 1099 C. Postcard-字符串处理(Codeforces Round #530 (Div. 2))

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
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.

Input

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 (1k2001≤k≤200), the required message length.

Output

Print any message of length kk that the given string can encode, or «Impossible» if such a message does not exist.

Examples
input
Copy
hw?ap*yn?eww*ye*ar
12
output
Copy
happynewyear
input
Copy
ab?a
2
output
Copy
aa
input
Copy
ab?a
3
output
Copy
aba
input
Copy
ababb
5
output
Copy
ababb
input
Copy
ab?a
1
output
Copy
Impossible

 

 

题意就是给你一个字符串,其中?可以有两种操作,1.将前面的字母放到当前位置,其实就是去掉?2.去掉前面的字母,其实就是去掉前面的字母去掉?

*的有三种操作,1.将前面的字母放到当前位置,就是去掉* 2.去掉前面的字母,就是去掉前面的字母和* 3.前面的字母可以重复任意次

假设是abc*,可以是abc,可以是ab,可以是abcccccc,就是这样的,给你一个字符串,问你能不能将字符串变成要求的长度,最后的字符串不能有?和*。

直接将纯字母的长度先找出来,然后判断再进行操作就可以了。

 

代码:

 1 //C
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<algorithm>
 6 #include<cmath>
 7 using namespace std;
 8 typedef long long ll;
 9 const int maxn=1e5+10;
10 
11 char s[200];
12 
13 int main()
14 {
15     scanf("%s",s);
16     int n;
17     cin>>n;
18     int len=strlen(s);
19     int candy=0,snow=0;
20     for(int i=0;i<len;i++){
21         if(s[i]=='?') candy++;
22         else if(s[i]=='*') snow++;
23     }
24     int l=len-candy-snow;
25     if(l==n){
26         for(int i=0;i<len;i++){
27             if(s[i]!='?'&&s[i]!='*') cout<<s[i];
28         }
29         cout<<endl;
30     }
31     else if(l<n&&snow>0){
32         int c;
33         for(int i=0;i<len;i++){
34             if(s[i]=='?') s[i]='#';
35             if(s[i]=='*'){
36                 if(l<n){
37                     c=n-l;
38                     s[i]='!';
39                     //cout<<s[i]<<endl;
40                     l=n;
41                 }
42                 else{
43                     s[i]='#';
44                 }
45             }
46         }
47         for(int i=0;i<len;i++){
48             if(s[i]!='#'){
49                 if(s[i]=='!'){
50                     for(int j=0;j<c;j++)
51                         cout<<s[i-1];
52                 }
53                 else cout<<s[i];
54             }
55         }
56         cout<<endl;
57     }
58     else if(l>n&&l-(candy+snow)<=n){
59         for(int i=0;i<len;i++){
60             if((s[i]=='?'||s[i]=='*')&&l>n){
61                 s[i-1]='#';
62                 s[i]='#';
63                 l--;
64             }
65             else if(s[i]=='?'||s[i]=='*')
66                 s[i]='#';
67         }
68         for(int i=0;i<len;i++){
69             if(s[i]!='#') cout<<s[i];
70         }
71         cout<<endl;
72     }
73     else cout<<"Impossible"<<endl;
74 }

 

 

 

 

转载于:https://www.cnblogs.com/ZERO-/p/10263722.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值