题意:给一个字符串,其中只有F、A、N三种字母,问最少交换多少次能使所有的A在所有F之前。

解法:贪心。先预处理每位的左边有多少F右边有多少A,对于每位A必须至少向左交换的次数为它左面的F个数,而对于N来说比较左面F的个数和右面A的个数,将少的部分交换至N的另一端。将以上两种情况的答案加和即为最后答案。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int main()
{
int n;
string s;
while(~scanf("%d", &n))
{
cin >> s;
int a[105] = {0}, f[105] = {0};
if(s[0] == 'F')
f[0] = 1;
int ans = 0;
for(int i = 1; i < n; i++)
{
if(s[i] == 'F')
f[i] = f[i - 1] + 1;
else
f[i] = f[i - 1];
}
for(int i = n - 1; i >= 0; i--)
{
if(s[i] == 'A')
{
a[i] = a[i + 1] + 1;
ans += f[i];
}
else
a[i] = a[i + 1];
}
for(int i = 0; i < n; i++)
{
if(s[i] == 'N')
ans += min(f[i], a[i]);
}
printf("%d\n", ans);
}
return 0;
}

  

CF GYM 100703K Word order的更多相关文章

  1. Gym 100703K Word order 贪心

    题目链接 题意:给定一个长度为n的字符串,字符串仅由"F","N","A"三种字符组成,现有一种操作P,即把两个相邻的字符调换位置.要求把所 ...

  2. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

  3. cf Gym 101086M ACPC Headquarters : AASTMT (Stairway to Heaven)

    题目: Description standard input/output As most of you know, the Arab Academy for Science and Technolo ...

  4. CF Gym 100685E Epic Fail of a Genie

    传送门 E. Epic Fail of a Genie time limit per test 0.5 seconds memory limit per test 64 megabytes input ...

  5. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  6. [CF] 8C Looking for Order

    状压模板题 CF难度2000? 我得好好了解一下CF的难度机制了 反正CF的难度比洛谷真实就好了 Code #include<algorithm> #include<iostream ...

  7. Gym 100431E Word Cover 题解:KMP上跑dp

    题意: 给你一个串,问你他的每个前缀的最小重复单元,其中单元是可以重叠的,最后按顺序输出即可.比如样例中abaabaa的最小重复单元为abaa,所以相应输出为4. 样例: input : abaaba ...

  8. CF Gym 100685A Ariel

    传送门 A. Ariel time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  9. CF GYM 100703A Tea-drinking

    题意:龙要制作n个茶,每个茶的配方是一个字符串,两个字符串之间有一个差值,这个差值为两个字符串每个对应字母之间差的绝对值的最大值,求制作所有茶时获得的所有差值中的最大值. 解法:克鲁斯卡尔.将茶的配方 ...

随机推荐

  1. 原生JS的对象常用操作总结

       前端时间写了篇怎么判断js对象相等的文章,一直在期待大神给点消息,无奈一直杳无音讯,还是自己写个函数来进行判断,下面总结一些常用的对象操作的方法.    咋们来个先抑后扬的方式,先放出几个基本的 ...

  2. 6779. Can you answer these queries VII - SPOJ

    Given a tree with N ( N<=100000 ) nodes. Each node has a interger value x_i ( |x_i|<=10000 ). ...

  3. (五)乱入之如何使用MNIST数据库

    (五)乱入之如何使用MNIST数据库 今天花了整整一天时间查各种资料,终于搞清楚了怎么使用MNIST数据库.哈哈,MNIST,是不是高端洋气上档次?是不是不知道是什么东东? MNIST是一个据说很出名 ...

  4. 团体程序设计天梯赛-练习集L1-020. 帅到没朋友

    L1-020. 帅到没朋友 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 当芸芸众生忙着在朋友圈中发照片的时候,总有一些人因为 ...

  5. XSS与CSRF两种跨站攻击比较

    XSS:跨站脚本(Cross-site scripting) CSRF:跨站请求伪造(Cross-site request forgery) 在那个年代,大家一般用拼接字符串的方式来构造动态SQL 语 ...

  6. Android中SQLite应用详解(转)

    上次我向大家介绍了SQLite的基本信息和使用过程,相信朋友们对SQLite已经有所了解了,那今天呢,我就和大家分享一下在Android中如何使用SQLite. 现在的主流移动设备像Android.i ...

  7. leetcode3 Two Sum III – Data structure design

    Question: Design and implement a TwoSum class. It should support the following operations: add and f ...

  8. javascript closure

    http://www.jibbering.com/faq/notes/closures/ http://hi.baidu.com/bluedream_119/item/938dcd082b1e1880 ...

  9. 【BZOJ 2300】 2300: [HAOI2011]防线修建 (动态凸包+set)

    2300: [HAOI2011]防线修建 Description 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上 ...

  10. SQL四种语言:DDL,DML,DCL,TCL

    1.DDL(Data Definition Language)数据库定义语言statements are used to define the database structure or schema ...