Codeforces 1099 C. Postcard-字符串处理(Codeforces Round #530 (Div. 2))
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,就是这样的,给你一个字符串,问你能不能将字符串变成要求的长度,最后的字符串不能有?和*。
直接将纯字母的长度先找出来,然后判断再进行操作就可以了。
代码:
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 }