Codeforces Round 915 (Div. 2) C题样例解释

Problem - C - Codeforces

看懂这个样例很重要:

15

czddeneeeemigec

AC代码:

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<stdio.h>
#include<utility>
#include<cstring>
#include<queue>
#include<stack>
#include<unordered_map>
#include<set>
#define ll long long 
const int MAX = 2e5 + 5;
int arr[MAX] = { 0 };
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int t;
	cin >> t;
	while (t--)
	{
		memset(arr, 0, sizeof arr);
		int n;
		cin >> n;
		string str;
		cin >> str;
		string msubstr;

		//找到最大子序列 —— 遍历n^2超时
		unordered_map<char, int>hash;
		for (char x : str)
			hash[x]++;
		for (int i = 0; i < str.size(); i++)
		{
			int j = str[i] + 1;
			for (; j <= 'z'; j++)
			{
				if (hash[j] != 0)
					break;
			}
			hash[str[i]]--;
			if (j <= 'z')continue;
			arr[i] = 1;//标记为子串
			msubstr += str[i];
		}
		//cout << "#### " << msubstr << endl;


		//其实就是翻转啊
		reverse(msubstr.begin(), msubstr.end());
		auto it = msubstr.begin();
		//改原串吧	
		for (int i = 0; i < str.size(); i++)
		{
			if (arr[i] == 1)
			{
				str[i] = *it;
				it++;
			}
		}
		//移动过才用比
		int i = 1;
		for (; i < str.size(); i++)
		{
			if (str[i] < str[i - 1])
				break;
		}
		if (i != str.size())
			cout << -1 << endl;
		else
		{
			/*if (msubstr[0] == msubstr.back())
				cout << 0 << endl;
			else*/
			//bdcdcb
			int s = 1;
			for (int j = msubstr.size() - 2; j >= 0; j--)
			{
				if (msubstr[j] == msubstr.back())
					s++;
				else
					break;
			}
			cout << msubstr.size() - s << endl;
		}
	}
	return 0;
}

相关推荐

  1. Codeforces Round 912 (Div. 2)补

    2023-12-21 10:18:03       38 阅读
  2. Codeforces Round 912 (Div. 2)

    2023-12-21 10:18:03       39 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-21 10:18:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-21 10:18:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-21 10:18:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-21 10:18:03       18 阅读

热门阅读

  1. 服务器不稳定因素

    2023-12-21 10:18:03       34 阅读
  2. js new Set()过滤重复数据

    2023-12-21 10:18:03       35 阅读
  3. Linux下修改host文件

    2023-12-21 10:18:03       36 阅读
  4. c语言突击函数

    2023-12-21 10:18:03       49 阅读
  5. 【数据爬取】Jsoup爬取数据的使用

    2023-12-21 10:18:03       40 阅读
  6. 支付宝单笔转账开发

    2023-12-21 10:18:03       43 阅读
  7. 拿到服务器该做的事和升级docker engine

    2023-12-21 10:18:03       43 阅读
  8. 记一次gunicorn启动报错

    2023-12-21 10:18:03       36 阅读