专栏文章
题解:P13189 [GCJ 2016 #1A] The Last Word
P13189题解参与者 1已保存评论 0
文章操作
快速查看文章及其快照的属性,并进行相关操作。
- 当前评论
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @miox4d98
- 此快照首次捕获于
- 2025/12/03 02:35 3 个月前
- 此快照最后确认于
- 2025/12/03 02:35 3 个月前
题目大意:
一个由大写英文字母组成的字符串 ,按顺序从中给出一个新字母,将其写在单词的开头或末尾,没有单词时直接写,根据字典序排序,并且需要排在最后,每次可将一个字母插入到最前面或最后面。
思路:
代码:
CPP#include<bits/stdc++.h>
using namespace std;
string s;
deque<char> q;
int main(){
int T;
scanf("%d",&T);
for(int i=1;i<=T;i++){
cin>>s;
int len=s.length();
for(int j=0;j<len;j++){
if(q.empty()){ // 队列空无法比较,直接加入。
q.push_back(s[j]);
}
else{
if(q.front()<=s[j]){ // 注意 ≤ 和字典序。
q.push_front(s[j]);
}
else{
q.push_back(s[j]);
}
}
}
printf("Case #%d: ",i);
while(!q.empty()){
char p=q.front();
printf("%c",p);
q.pop_front();
}
printf("\n");
}
return 0;
}
相关推荐
评论
共 0 条评论,欢迎与作者交流。
正在加载评论...