Gym 100703K Word order 贪心

题目链接

题目大意:给定一个长度为n的字符串,字符串仅由"F","N","A"三种字符组成,现有一种操作P,即把两个相邻的字符调换位置。要求把所有的A都放在所有的F左侧,问需要的最少操作P的次数。

思路:首先从左至右的扫描原串,对于每一个"A",设它的左侧有x个"F",则必然至少需要x次操作将"A"换到"F"的左侧或者把“F”换到"A"的右侧。然后对于每个"N",设它左侧有L_F个"F",右侧有R_A个"A",若L_F大于R_A,则更优解就是把"A"换到左侧,而反之,则是把"F"换到右侧。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
#define MAXN 110
char str[MAXN];
int n;
int main() {
    scanf("%d", &n);
    int ans = 0;
    scanf("%s", str + 1);
    int F_num = 0;
    for(int i = 1; i <= n; i++) {
        if(str[i] == ‘F‘) F_num++;
        else if(str[i] == ‘A‘) ans += F_num;
    }
    for(int i = 1; i <= n; i++) {
        if(str[i] != ‘N‘) continue;
        int st = i, ed = i;
        while(str[ed] != ‘N‘) ed++;
        int len = ed - st + 1;
        int L_F = 0, R_A = 0;
        for(int j = 1; j < st; j++)
            if(str[j] == ‘F‘) L_F++;
        for(int j = ed + 1; j <= n; j++)
            if(str[j] == ‘A‘) R_A++;
        ans += len * min(L_F, R_A);
    }
    printf("%d\n", ans);
    return 0;
}
时间: 2024-08-06 16:06:28

Gym 100703K Word order 贪心的相关文章

CF GYM 100703K Word order

题意:给一个字符串,其中只有F.A.N三种字母,问最少交换多少次能使所有的A在所有F之前. 解法:贪心.先预处理每位的左边有多少F右边有多少A,对于每位A必须至少向左交换的次数为它左面的F个数,而对于N来说比较左面F的个数和右面A的个数,将少的部分交换至N的另一端.将以上两种情况的答案加和即为最后答案. 代码: #include<stdio.h> #include<iostream> #include<algorithm> #include<string>

Gym 100431E Word Cover 题解:KMP神用

题意: 给你一个串,问你他的每个前缀的最小重复单元,其中单元是可以重叠的,最后按顺序输出即可.比如样例中abaabaa的最小重复单元为abaa,所以相应输出为4. 样例: input : abaabaababa outpit:1 2 3 4 5 3 4 5 3 10 3 kmp过程就不用多说了,现在我们利用next数组的性质来对问题进行求解. 我们首先用一个ans[maxn]数组来记录最后的答案,且我们的字符串下标从0开始,显然,我们ans[i]的最大值为i+1,我们因此也用此值对其进行初始化.

CodeForces Gym 100685I Innovative Business (贪心)

题意:给定一条路的长和宽,然后给你瓷砖的长和宽,你只能横着或者竖着铺,也可以切成片,但是每条边只能对应一条边,问你最少要多少瓷砖. 析:先整块整块的放,然后再考虑剩下部分,剩下的再分成3部分,先横着,再竖着,最后是他们相交的部分. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdli

[Hive_add_6] Hive 实现 Word Count

0. 说明 1. Hive 实现 Word Count 方式一 1.1 思路 将每一行文本变为 Array 数组的一个元素 再通过 collection items terminated by ' ' 完成转换单行文本 最后通过表生成函数 explode 分裂 array 数组中的元素变成多行 1.2 实现 1. 创建表 wc create table wc(line array<string>) row format delimited collection items terminated

Sentiment Analysis(1)-Dependency Tree-based Sentiment Classification using CRFs with Hidden Variables

The content is from this paper: Dependency Tree-based Sentiment Classification using CRFs with Hidden Variables, by Tetsuji Nakagawa. A typical approach for sentiment classification is to use supervised machine learning algorithms with bag-of-words a

递归+解析 SRM 593 Division Two - Level Two: WolfDelaymaster

WolfDelaymaster Problem Statement Wolf Sothe is playing the game Delaymaster. In this game, he can create new words according to the following rules: For each positive integer n, the string which consists of n copies of 'w', then n copies of 'o', the

557. Reverse Words in a String III

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"

leetcode_557 Reverse Words in a String III(String)

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc"

Shell命令行

利用wc命令统计文件行,单词数,字符数,利用sort排序和去重,再结合uniq可以进行词频统计. cat file.txt sort hello.c | uniq -c | sort -nr |head -5 用cat命令查看文件格式与内容.先对文件进行排序,再用uniq -c命令统计不同单词及各个单词出现的次数,得到的结果就是次数后面紧挨的单词,然后使用sort -nr对次数进行排序,并逆序显示,最后head -5命令显示结果的前5行. 类似于sql语句: select word,count(