codeforces 55 div2 C.Title 模拟
Vasya has recently finished writing a book. Now he faces the problem of giving it the title. Vasya wants the title to be vague and mysterious for his book to be noticeable among others. That's why the title should be represented by a single word containing at least once each of the first k Latin letters and not containing any other ones. Also, the title should be a palindrome, that is it should be read similarly from the left to the right and from the right to the left.
Vasya has already composed the approximate variant of the title. You are given the title template s consisting of lowercase Latin letters and question marks. Your task is to replace all the question marks by lowercase Latin letters so that the resulting word satisfies the requirements, described above. Each question mark should be replaced by exactly one letter, it is not allowed to delete characters or add new ones to the template. If there are several suitable titles, choose the first in the alphabetical order, for Vasya's book to appear as early as possible in all the catalogues.
The first line contains an integer k (1 ≤ k ≤ 26) which is the number of allowed alphabet letters. The second line contains s which is the given template. In s only the first k lowercase letters of Latin alphabet and question marks can be present, the length of s is from 1 to 100 characters inclusively.
If there is no solution, print IMPOSSIBLE. Otherwise, a single line should contain the required title, satisfying the given template. The title should be a palindrome and it can only contain the first k letters of the Latin alphabet. At that, each of those k letters must be present at least once. If there are several suitable titles, print the lexicographically minimal one.
The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller than the line b, if exists such an i (1 ≤ i ≤ |s|), that ai < bi, and for any j (1 ≤ j < i) aj = bj. |s| stands for the length of the given template.
3
a?c
IMPOSSIBLE
2
a??a
abba
2
?b?a
abba
题意:需要使用n个字符得到一个字典序最小的回文串;
思路:先预处理出含有的字母,跟?是不是根据回文可以得到;
从中间往前遍历,如果可以用未出现的最大的字母填入,否则用最小的字母填;
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define inf 100000000000005 #define MAXN 10000010 #define pi 4*atan(1) #define esp 0.000000001 //#pragma comment(linker, "/STACK:102400000,102400000") char a[100010]; int flag[30],ans; void init(int len) { for(int i=0;i<=len/2;i++) { if(a[i]=='?'&&a[len-i-1]!='?') a[i]=a[len-i-1],flag[a[len-i-1]-'a']=1; if(a[i]!='?'&&a[len-i-1]=='?') a[len-i-1]=a[i],flag[a[i]-'a']=1; if(a[i]!='?'&&a[len-i-1]!='?') flag[a[i]-'a']=1; } for(int i=0;i<len/2;i++) { if(a[i]!=a[len-i-1]&&a[i]!='?') { ans=1; return; } } } int getsum(int x) { int sum=0; for(int i=0;i<x;i++) sum+=flag[i]; return sum; } int first(int x) { int i; for(i=x-1;i>0;i--) { if(flag[i]==0) return i; } return i; } int main() { int x,y,z,i,t; ans=0; scanf("%d",&x); scanf("%s",a); int len=strlen(a); init(len); if(ans) { printf("IMPOSSIBLE\n"); return 0; } for(i=len/2;i>=0;i--) { if(a[i]=='?') { z=first(x); a[i]=a[len-i-1]=z+'a'; flag[z]=1; } } if(getsum(x)==x) printf("%s\n",a); else printf("IMPOSSIBLE\n"); return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步