bestcoder#37_1001 字符串,贪心

bestcoder#37_1001 字符串,dfs

Rikka with string

Accepts: 395

Submissions: 2281

Time Limit: 2000/1000 MS (Java/Others)

Memory Limit: 65536/65536 K (Java/Others)

Problem Description

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

One day, Yuta got a string which contains n letters but Rikka lost it in accident. Now they want to recover the string. Yuta remembers that the string only contains lowercase letters and it is not a palindrome string. Unfortunately he cannot remember some letters. Can you help him recover the string?

It is too difficult for Rikka. Can you help her?

Input

This problem has multi test cases (no more than 20). For each test case, The first line contains a number n(1≤n≤1000). The next line contains an n-length string which only contains lowercase letters and ‘?’ – the place which Yuta is not sure.

Output

For each test cases print a n-length string – the string you come up with. In the case where more than one string exists, print the lexicographically ?rst one. In the case where no such string exists, output “QwQ”.

Sample Input

5
a?bb?
3
aaa

Sample Output

aabba
QwQ题意:在问号中填上字母,使字符串不是回文且字典序最小思路:贪心,考虑到字典序最小,先扫一遍贪心地在问号处填上‘a‘,再扫一遍判断是否回文,如果不是直接输出,如果是回文,考虑到字典序最小,只需更改最右端的问号处的a为b即可输出,但如果最右端的问号恰好在字符串的中点,特判跳过即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>

using namespace std;

const int maxn=1000100;
const int INF=(1<<29);

int n;
string s;
vector<int> v;

bool ispalin(string s)
{
    for(int i=0;i<s.length();i++){
        if(s[i]!=s[n-1-i]) return false;
    }
    return true;
}

int main()
{
    while(cin>>n>>s){
        v.clear();
        bool flag=0;
        for(int i=0;i<n;i++){
            if(s[i]==‘?‘){
                s[i]=‘a‘;
                v.push_back(i);
            }
        }
        if(!ispalin(s)) cout<<s<<endl;
        else{
            for(int i=(int)v.size()-1;i>=0;i--){
                if(v[i]!=n/2||n%2==0){
                    s[v[i]]=‘b‘;
                    flag=1;
                    break;
                }
            }
            if(flag) cout<<s<<endl;
            else cout<<"QwQ"<<endl;
        }
    }
    return 0;
}

时间: 2024-07-31 18:15:59

bestcoder#37_1001 字符串,贪心的相关文章

hihocoder-1327-分割字符串--贪心

hihocoder-1327-分割字符串--贪心 1327 : 分隔相同字符 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个只包含小写字母'a'-'z'的字符串 S ,你需要将 S 中的字符重新排序,使得任意两个相同的字符不连在一起. 如果有多个重排后字符串满足条件,输出字典序最小的一个. 如果不存在满足条件的字符串,输出INVALID. 输入 字符串S.(1 ≤ |S| ≤ 100000) 输出 输出字典序最小的答案或者INVALID. 样例输入 aaa

【LeetCode】 -- 68.Text Justification

题目大意:给定一个数组容器,里面存有很多string: 一个int maxWith.让你均匀安排每一行的字符串,能够尽可能的均匀. 解题思路:字符串+贪心.一开始想复杂了,总觉的题意描述的不是很清楚,其实放到实际的场景中去,无非就是想让前端字符串布局变得更加美观,而设计的字符串对其方式(分散对齐)(如果每一行只有一个字符串的话,那么左对齐). 附上代码: 1 vector<string> fullJustify(vector<string>& words, int maxW

uva--1368(贪心,字符串模拟)

点击打开链接 该题是一个带有贪心思想的字符串模拟题,题目给定m个长度为n的字符串,让你求一个长度为n的字符串,使得该字符串与这m个字符串对应位置的字符不同的个数和最小. 要使对应位置不同字符最少,则该字符串每个字符优先选择该位置出现次数多的字符,若次数相同则选择字典序更小的字符. 代码: #include <iostream> #include <cstdio> #include <string.h> #include <map> #include <

贪心基础入门讲解一——完美字符串

约翰认为字符串的完美度等于它里面所有字母的完美度之和.每个字母的完美度可以由你来分配,不同字母的完美度不同,分别对应一个1-26之间的整数. 约翰不在乎字母大小写.(也就是说字母F和f)的完美度相同.给定一个字符串,输出它的最大可能的完美度.例如:dad,你可以将26分配给d,25分配给a,这样整个字符串完美度为77. 分析: 由排序不等式,出现次数最多的字母显然应该给26.所以这个题目变成了统计每种字母出现的次数了,然后按照出现次数从大到小,依次分配从高到低的权值.这就是最朴素的贪心思想. 输

[BestCoder Round #4] hdu 4932 Miaomiao&#39;s Geometry (贪心)

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 92 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

poj 3617 Best Cow Line (字符串反转贪心算法)

Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9284   Accepted: 2826 Description FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of the Year" competition. In this contest every farmer arranges his

2017 Multi-University Training Contest - Team 1 1002&amp;&amp;HDU 6034 Balala Power!【字符串,贪心+排序】

Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2668    Accepted Submission(s): 562 Problem Description Sample Input 1 a 2 aa bb 3 a ba abc Sample Output Case #1: 25 Case #2: 132

字符串处理 BestCoder Round #43 1001 pog loves szh I

题目传送门 1 /* 2 字符串处理:是一道水题,但是WA了3次,要注意是没有加'\0'的字符串不要用%s输出,否则在多组测试时输出多余的字符 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 const int MAXN = 1

贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

题目传送门 1 /* 2 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 3 当然有可能两个数和超过p,那么an的值最优,每次还要和an比较 4 注意:不能选取两个相同的数 5 反思:比赛时想到了%p和sort,lower_bound,但是还是没有想到这个贪心方法保证得出最大值,还是题目做的少啊:( 6 */ 7 #include <cstdio> 8 #include <algorithm>