M - Two Operations

M - Two Operationshttps://vjudge.csgrandeur.cn/problem/Gym-102263M

Ayoub has a string SS consists of only lower case Latin letters, and he wants you to make some operations on it:

  1. you can swap any two characters in the string.
  2. you can delete any two adjacent characters that have the same value and replace them with the next character alphabetically,for example string "abbx""abbx" could be "acx""acx" after one operation, string "zz""zz" could not be changed; because z is the last character in the English alphabets.

Ayoub wants you to make the string lexicographically maximal using the mentioned operations as many times as you want, can you help him?

String x=x1x2...x|x|x=x1x2...x|x| is lexicographically larger than string y=y1y2...y|y|y=y1y2...y|y|, if either |x|>|y||x|>|y| and x1=y1,x2=y2,...,x|y|=y|y|x1=y1,x2=y2,...,x|y|=y|y|, or exists such number r(r<|x|,r<|y|)r(r<|x|,r<|y|), that x1=y1,x2=y2,...,xr=yrx1=y1,x2=y2,...,xr=yr and xr+1>yr+1xr+1>yr+1. Characters in lines are compared like their ASCII codes.

Input

The input contains a single string SS (1≤|S|≤105)(1≤|S|≤105).

It is guaranteed that string SS consists of only lower case Latin letters.

Output

print the lexicographically maximal string that could be obtained using these two operations.

Examples

input

Copy

abbx

output

Copy

xca

input

Copy

zyayz

output

Copy

zzza

Note

In the first test case Ayoub replaced "bb""bb" with "c""c" so the string has been changed to "acx""acx", then he swapped 'a' with 'x' so the result is "xca""xca" and it is the lexicographically maximal string.

正解:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int vis[N];
int main()
{
    string s;
    cin>>s;
    int len=s.size();
    for(int i=0;i<len;i++)
    {
        vis[s[i]]++;
    }
    for(int i=97;i<=121;i++)
    {
        vis[i+1]=vis[i+1]+vis[i]/2;//转换完成后加到下一组
        vis[i]=vis[i]%2;//vis[i]剩下的
    }
    for(int i=122;i>=97;i--)
    {
        while(vis[i]--)
        {
            printf("%c",i);
        }
    }
    cout<<endl;
}

错解:

#include<bits/stdc++.h>
using namespace std;
bool cmp(char a,char b)
{
    return a>b;
}
int main()
{
    char s[10010];
    int n;
    map<char,int>m;
    scanf("%s",s);
    n=strlen(s);
    sort(s,s+n,cmp);
    for(int i=0;i<n;i++)
    {
        m[s[i]]++;
    }
    for(int i=0;i<n;i++)
    {
        if(s[i]=='z')
            printf("z");
        else
        {
            int num=m[s[i]];
            if(num%2==0)
            {
                num=num/2;
                while(num--)
                {
                    printf("%c",s[i]+1);
                }
                m[s[i]]=0;
            }
            else
            {
                num=num/2;
                while(num--)
                {
                    printf("%c",s[i]+1);
                }
                printf("%c",s[i]);
                m[s[i]]=0;
            }
        }
    }
    return 0;
}

感觉这题没说清楚,没说转换完后还可以再转换

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题涉及到一个货币兑换的问题,需要我们输入一些数据,然后判断是否能够增加货币的数量。 下面是用 C++ 解决这个问题的代码: ```c++ #include <iostream> #include <cstring> using namespace std; const int MAXN = 100 + 10; const double INF = 1e9; int n, m, S; double V, d[MAXN][MAXN]; struct Exchange { int u, v; double r, c; } e[MAXN]; void floyd() { for (int k = 1; k <= m; k++) { for (int i = 1; i <= m; i++) { for (int j = 1; j <= m; j++) { if (d[i][k] * e[k].r - e[k].c > 0) { d[i][j] = max(d[i][j], d[i][k] * e[k].r - e[k].c); } } } } } int main() { cin >> n >> m >> S >> V; for (int i = 1; i <= m; i++) { cin >> e[i].u >> e[i].v >> e[i].r >> e[i].c; } memset(d, 0, sizeof(d)); for (int i = 1; i <= m; i++) { d[e[i].u][e[i].v] = V * e[i].r - e[i].c; } floyd(); bool flag = false; for (int i = 1; i <= n; i++) { if (d[S][i] > V) { flag = true; break; } } if (flag) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; } ``` 具体思路如下: 1. 读入数据,包括货币的数量 $n$,兑换点的数量 $m$,货币 Nick 拥有的编号 $S$ 和数量 $V$,以及每个兑换点的描述。 2. 初始化一个邻接矩阵 $d$,表示每个货币对其他货币的兑换情况。初始值为 0,表示不需要兑换。 3. 对于每个兑换点 $k$,更新邻接矩阵 $d$,其中 $d_{i,j}$ 表示从货币 $i$ 兑换到货币 $j$ 的最大收益。如果从 $i$ 到 $j$ 经过 $k$ 的收益大于 0,那么更新 $d_{i,j}$。 4. 使用 Floyd 算法更新所有货币对之间的最大收益。 5. 判断是否存在一种兑换方案,使得从货币 $S$ 开始到其他货币的收益均大于 $V$。如果存在,则输出 YES,否则输出 NO。 注意,在更新 $d$ 的时候,$d_{i,j}$ 的初始值应该为 $V \times e_k.r - e_k.c$,表示从货币 $i$ 到货币 $j$ 经过兑换点 $k$ 的兑换后的收益。如果 $d_{i,j}$ 在更新后仍然为 0,表示从货币 $i$ 到货币 $j$ 没有找到任何一种兑换方式,即无法从货币 $i$ 兑换到货币 $j$。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值