今天的题有点难度,但不多
一、牛牛冲钻五
说白了其实就是找“WWW”,特殊算
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
while (n--)
{
int x, k;
cin >> x >> k;
char* a = new char[x+1];
cin >> a;
long long count = 0;
int i = 0;
for (i; i < x; i++)
{
if (a[i] == 'L')
{
count--;
}
else
{
if (i - 2 >= 0)
{
if (a[i - 1] == 'W' && a[i - 2] == 'W')
count += k;
else
count++;
}
else
count++;
}
}
cout << count<<endl;
}
}
二、最长无重复子串
开始的位置记为0,往后找,如果遇到重复的,该重复数字对应的最小位置的后一位为新的开始(就是不要让重复的包含进去)
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param arr int整型一维数组 the array
* @param arrLen int arr数组长度
* @return int整型
*/
#include<stdio.h>
int maxLength(int* arr, int arrLen) {
int i = 0;
int count = 0;
int start = 0;
for (i; i < arrLen; i++)
{
int ct = 0;
int j = start;
int flag = 0;
for (j = start; j < i; j++)
{
if (arr[i] == arr[j])
{
start = j + 1;
break;
}
else
ct++;
}
if (j == i)
ct++;
if (ct > count)
count = ct;
}
return count;
}
三、重排字符串
这道题是今天最难的一道,先把各个字母出现的次数记一遍,找最大,如果大于个数一般,直接‘no’,然后考虑小于,直接按顺序记在数组arr,arr后面八成有相等的,所以在搞一个arr2,把最后的往前找合适位置插入。
#include <iostream>
using namespace std;
int number[27] = { 0 };
char arr[1000002] = { 0 };
char arr2[1000002] = { 0 };
char s[1000002] = { 0 };
int main() {
int n;
cin >> n;
int i = 0;
cin >> s;
int max = 0;
char maxc = 0;
char minc = 0;
for (i = 0; i < n; i++)
{
if (i == 0 || s[i] < minc)
minc = s[i];
number[s[i] - 'a']++;
if (number[s[i] - 'a'] > max)
{
maxc = s[i];
max = number[s[i] - 'a'];
}
}
if (max >= (n + 1) / 2 + 1 && max > 1)
cout << "no";
else
{
cout << "yes" << endl;
int flag = 1;
int ct = 0;
if (minc != maxc)
{
arr[ct++] = maxc;
number[maxc - 'a']--;
}
while (flag)
{
flag = 0;
int t = 0;
for (t; t < 27; t++)
{
if (number[t] != 0)
{
arr[ct++] = t + 'a';
flag = 1;
number[t]--;
}
}
}
int flagk = 1;
char* now = arr;
char* cur = arr2;
while (now[ct - 1] == now[ct - 2])//解决后面相邻相等问题
{
flagk = 1;
int my = 0;
int k = 0;
for (k; k < ct - 1; k++)
{
if (now[k] != now[ct - 1] && now[k + 1] != now[ct - 1] && flagk)
{
cur[my++] = now[k];
cur[my++] = now[ct - 1];
flagk = 0;
}
else
cur[my++] = now[k];
}
//cout << arr2;
char* tmp = now;
now = cur;
cur = tmp;
}
cout << now;
}
}