【强训笔记】day13

NO.1
在这里插入图片描述
代码实现:

 #include <iostream>
 #include<string>
 using namespace std;
 
int n,k,t;
string s;

int func()
{
    int ret=0;
    for(int i=0;i<n;i++)
    {
        char ch=s[i];
        if(ch=='L') ret-=1;
        else
        {
            if(i-1>=0&&i-2>=0&&s[i-1]=='W'&&s[i-2]=='W') ret+=k;
            else
            {
                ret+=1;
            }
        }
}
    return ret;
}

int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>k>>s;
        cout<<func()<<endl;
    }
    return 0;
}

NO.2
在这里插入图片描述
思路:双指针+滑动窗口,哈希判断是否有重复字符,如果有就出窗口,出窗口就更新长度,right++继续遍历。
代码实现:

class Solution {
public:
    int hash[100010]={0};
    int maxLength(vector<int>& arr) {
        int left=0,right=0;
        int n=arr.size();
        int ret=0;
        while(right<n)
        {
            hash[arr[right]]++;
            while(hash[arr[right]]>1)
            {
                hash[arr[left]]--;
                left++;
            }
            ret=max(ret,right-left+1);
            right++;

        }
        return ret;
    }
};

NO.3
在这里插入图片描述
在这里插入图片描述
思路:用哈希表统计出现次数最多的字符,再判断是否可以重排,如果次数大于(n+1)/2就不能重排,再处理出现次数最多的字符,间隔一个就摆放,如果摆放的位置大于n,就从1开始摆放,也是间隔一个格子,最后输出就可以了。

代码实现:

#include<iostream>

using namespace std;

const int N=1e5+10;
char s[N];
char ret[N];
int n;

int main()
{
    cin>>n>>s;
    int hash[26]={0};
    char maxchar=0;
    int maxcount=0;
    
    for(int i=0;i<n;i++)
    {
        int index=s[i]-'a';
        if(++hash[index]>maxcount)
        {
          maxchar=s[i];
        maxcount=hash[index];
            
        }
    }
    
    if(maxcount>(n+1)/2) cout<<"no"<<endl;
    else{

        cout<<"yes"<<endl;
        int i=0;
        while(maxcount--)
        {
         ret[i]=maxchar;
            i+=2;
        }
        for(int j=0;j<26;j++)
        {
            if(hash[j]&&j+'a'!=maxchar)
            {
                while(hash[j]--)
                {
                    if(i>=n) i=1;
                    ret[i]=j+'a';
                    i+=2;
                }
            }
        }
        for(int j=0;j<n;j++) cout<<ret[j];
        cout<<endl;
    }

    return 0;
}
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Lehjy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值