题意:给一个字符串,其中只有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. Object c中的alloc和init问题

    从开始学的NSString *name=[[NSString alloc] init] 起,老师教这句话是分配内存空间,一直在用,从来没考虑过它的内部是怎么实现的.今天无意中看到了这一句代码 NSSt ...

  2. 【学习总结】Info.plist和pch文件的作用

      Info.plist   建立一个工程后,会在Supporting files文件夹下看到一个“Info.plist”的文件,该文件对工程做一些运行期的配置,非常重要,不能删除 项目中其他Plis ...

  3. DB天气app冲刺第十二天

    今天其实不算冲刺了 ,因为今天没怎么花时间在软件上,而是花时间在老师留的作业上了.所以也算作是软件工程这门课的冲刺吧. DB天气这款app上今天的api接口还是木有弄好.明天会继续弄.但是全国城市的数 ...

  4. 使用Yeoman搭建 AngularJS 应用 (10) —— 让我们搭建一个网页应用

    原文地址:http://yeoman.io/codelab/write-unit-tests.html 对于不熟悉的Karma的人来说,这是JavaScript测试框架,这个Angular的生成器包含 ...

  5. iOS 网络编程

    iOS 开发中所需的数据基本都是来自网络,网络数据请求是 iOS 编程中必不可少的,应该熟练掌握网络请求. 网络请求方式有 :GET , POST , PUT ,DELETE 等,其中常用的就是 GE ...

  6. httpmime-session 会话保持

    sesion在浏览器和web服务器直接是通过一个叫做name为sessionid的cookie来传递的,所以只要在每次数据请求时保持sessionid是同一个不变就可以用到web的session了,做 ...

  7. [scalability] Find all documents that contain a list of words

    Given a list of millions of documents, how would you find all documents that contain a list of words ...

  8. 对jQuery.isArray方法的分析

    jQuery.isArray方法应于判断是不是数组,是的话返回true,否则返回false.调用如:jQuery.isArray([]),返回true.其实现源码如下: isArray: Array. ...

  9. properties配置文件中文乱码解决方法

    方法1   properties文件的格式一般为: ROOT=http://localhost:8080/BNCAR2/ ROOTPATH=E:/ws2/BNCAR2/rel/ MALL_PARTS_ ...

  10. express 3.0.x 中默认不支持layout.ejs的解决方法

    1.第一种方法用include 用<% include 模板名(可不加.ejs) %>的写法,具体如下 <% include header %> //套用布局拆成两部分 hea ...