当前位置:网站首页>Problem - 922D、Robot Vacuum Cleaner - Codeforces
Problem - 922D、Robot Vacuum Cleaner - Codeforces
2022-07-06 16:15:00 【Hello_ Ä】
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Pushok the dog has been chasing Imp for a few hours already.
Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner.While moving, the robot generates a string t consisting of letters ‘s’ and ‘h’, that produces a lot of noise. We define noise of string t as the number of occurrences of string “sh” as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and and .The robot is off at the moment. Imp knows that it has a sequence of strings ti in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.Help Imp to find the maximum noise he can achieve by changing the order of the strings.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of strings in robot’s memory.Next n lines contain the strings t1, t2, …, tn, one per line. It is guaranteed that the strings are non-empty, contain only English letters ‘s’ and ‘h’ and their total length does not exceed 105.
Output
Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings.
Examples
input
4
ssh
hs
s
hhhs
output
18
Problem analysis
The question is , Here you are. n A string , These strings are all composed of characters ’s’ and ‘h’ Composed of . Let you combine these strings in any order , Make the string contain ’sh‘ Subsequences are at most .
About this problem , It uses a kind of called “ Neighborhood exchange ” Written by , The idea is like this : Two strings a and b, If a+b The composed string contains ’sh‘ Subsequence ratio b+a The string consists of many , Then when you finally splice a Put it in b The former is a better choice . So this method is to write a cmp then sort Just a moment .
cmp The content is to combine the two strings in different order , Calculate what they contain respectively ’sh‘ Number , as for ’sh‘ Number of sequences , It's every one h All before the character s The number of characters adds up . Pay attention to the , If two strings of ’sh‘ The number of sequences is equal , Then go back false( I don't know why , But that's it , Otherwise re).
AC Code
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#define endl '\n'
#define int ll
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50;
int get_sh(string& s)
{
int cnt = 0, nums = 0, numh = 0;
for (auto i : s)
{
if (i == 's')
{
nums++;
}
else
{
numh++;
cnt += nums;
}
}
return cnt;
}
bool cmp(string a, string b)
{
string s1 = a + b, s2 = b + a;
return get_sh(s1)>get_sh(s2);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n;
cin >> n;
vector<string>v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
sort(v.begin(), v.end(),cmp);
string s;
for (auto i : v)s += i;
cout << get_sh(s) << endl;
return 0;
}
ending
Adjacent term exchange method is also a common greedy method , Similar topics include Algorithm improvement Minimum string and New king game - subject - Daimayuan Online Judge
边栏推荐
- 875. 爱吃香蕉的珂珂 - 力扣(LeetCode)
- 409. Longest palindrome
- 读取和保存zarr文件
- Codeforces Round #799 (Div. 4)A~H
- Suffix expression (greed + thinking)
- b站 實時彈幕和曆史彈幕 Protobuf 格式解析
- Understand what is a programming language in a popular way
- window11 conda安装pytorch过程中遇到的一些问题
- Penetration test (3) -- Metasploit framework (MSF)
- Auto.js入门
猜你喜欢
Penetration test (3) -- Metasploit framework (MSF)
“鬼鬼祟祟的”新小行星将在本周安全掠过地球:如何观看
The "sneaky" new asteroid will pass the earth safely this week: how to watch it
1005. Maximized array sum after K negations
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Browser print margin, default / borderless, full 1 page A4
Penetration test (4) -- detailed explanation of meterpreter command
QT有关QCobobox控件的样式设置(圆角、下拉框,向上展开、可编辑、内部布局等)
2078. Two houses with different colors and the farthest distance
1605. Sum the feasible matrix for a given row and column
随机推荐
Flask框架配置loguru日志庫
滲透測試 ( 1 ) --- 必備 工具、導航
读取和保存zarr文件
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
[exercise-6] (UVA 725) division = = violence
input 只能输入数字,限定输入
Codeforces Round #798 (Div. 2)A~D
Pytorch extract skeleton (differentiable)
605. Planting flowers
Problem - 922D、Robot Vacuum Cleaner - Codeforces
Write web games in C language
[exercise-5] (UVA 839) not so mobile (balance)
Codeforces - 1526C1&&C2 - Potions
Codeforces Round #800 (Div. 2)AC
C language is the watershed between low-level and high-level
(POJ - 3579) median (two points)
Configuration du cadre flask loguru log Library
1005. Maximized array sum after K negations
Penetration test (7) -- vulnerability scanning tool Nessus
[exercise-4] (UVA 11988) broken keyboard = = (linked list)