2071. Juice Cocktails

Time limit: 1.0 second
Memory limit: 64 MB
Once n Denchiks come to the bar and each orders a juice cocktail. It could be from 1 to 3 different juices in each cocktail. There are three juices in the bar: apple, banana and pineapple juice.
Every Denchik has his favourite juice cocktail. The cocktail is uniquely defined by those kinds of juices which it contains. Of course, each Denchik ordered his favorite cocktail. The barman can put all n glasses in a long row and pour the juice in the segment of successive glasses.
The bar is very popular, so the barman wants to serve Denchiks as quickly as possible. So, he wants to arrange glasses in a certain way to minimize the amount of "pourings." For one "pouring" he can pour one kind of juice in one segment of successive glasses.
As it is known, apple juice has higher density than banana juice. And density of banana juice is higher than that of pineapple one. Therefore, not to mix juices in a cocktail, you should firstly pour the apple, then banana and finally pineapple juice.
Of course, you must pour only that juices into each glass which are parts of a certain cocktail, but the relative order of juices should remain unchanged. There‘re too many Denchiks, so the barman can‘t understand how should he arrange glassess. Could you?

Input

The first line contains integer n (1 ≤ n ≤ 105).
Next n lines describe Denchiks‘orders. Each order is described by one line, which consists of from 1 to 3 title Latin letters — A, B, P. The i-th line gives the kinds of juice included in the i-th order (A = Apple, B = Banana, P = Pineapple). The letters inside the string are arranged in this only order — "ABP", so to make the string you should eliminate some letters (perhaps, none) from "ABP" combination.

Output

In the first line print the minimal number of "pourings."
In the second line output the indices of glasses in order in which they have to be arranged in row. Assume that the orders and corresponding glasses are numbered from 1 to n in the input order.
If there are several optimal ways of glasses arrangement, output any of them.

Sample

input output
3
A
B
ABP
3
1 3 2
Problem Author: Mikhail Rubinchik (prepared by Nickolai Permyakov, Alexey Danilyuk)
Problem Source: Ural Regional School Programming Contest 2015
Difficulty: 428
 
题意:给出n个串,只由A、B、P三个字母组成,且顺序是ABP,即它们的相对顺序就是ABP
每次可以消去连续一些串的相同的首字母,比如
ab   ->     b
ap      p
问在最开始自有安排它们顺序后,最少多少次可以消去全部,输出方案
 
分析:
首先相同构成的肯定放在一起
一共只有7种串,暴力枚举他们的排列,然后暴力贪心的消去
 #include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
#define INF (1000000001)
#define sz(x) ((int) (x).size())
#define pub push_back const int N = ;
const string STORE[] = {"A", "AB", "B", "BP", "ABP", "AP", "P"};
int n, m;
string data[N];
vector<int> cnt[];
int arr[N], it[N], pos[];
string origin[], tmp[];
int ansPos[]; inline void input()
{
cin >> n;
for(int i = ; i < n; i++) cin >> data[i];
} inline int work()
{
char ch[] = {'A', 'B', 'P'};
int it[] = {}, ret = ;
for(int t = ; t < ; t++)
for(int i = ; i < m; i++)
if(tmp[i][it[i]] == ch[t])
{
it[i]++;
int j = i;
while(j + < m)
{
if(it[j + ] >= sz(tmp[j + ])) break;
if(tmp[j + ][it[j + ]] != ch[t]) break;
j++;
it[j]++;
}
ret++;
i = j;
}
return ret;
} inline int num(string &str)
{
int x;
if(str == "A") x = ;
else if(str == "AB") x = ;
else if(str == "B") x = ;
else if(str == "BP") x = ;
else if(str == "ABP") x = ;
else if(str == "AP") x = ;
else if(str == "P") x = ;
return x;
} inline bool cmp(string a, string b)
{
int x = num(a), y = num(b);
return x < y;
} inline void solve()
{
for(int i = ; i < n; i++)
if(data[i] == "A") cnt[].pub(i);
else if(data[i] == "AB") cnt[].pub(i);
else if(data[i] == "B") cnt[].pub(i);
else if(data[i] == "BP") cnt[].pub(i);
else if(data[i] == "ABP") cnt[].pub(i);
else if(data[i] == "AP") cnt[].pub(i);
else if(data[i] == "P") cnt[].pub(i); m = ;
for(int i = ; i < ; i++)
if(sz(cnt[i]))
{
pos[m] = i;
m++;
}
int ans = INF;
do
{
for(int i = ; i < m; i++) tmp[i] = STORE[pos[i]];
int o = work();
if(ans > o)
{
ans = o;
for(int i = ; i < m; i++) ansPos[i] = pos[i];
}
} while(next_permutation(pos, pos + m)); int out = ;
printf("%d\n", ans);
for(int i = ; i < m; i++)
for(int j = ; j < sz(cnt[ansPos[i]]); j++)
{
out++;
if(out == n) printf("%d\n", cnt[ansPos[i]][j] + );
else printf("%d ", cnt[ansPos[i]][j] + );
}
} int main()
{
ios::sync_with_stdio();
input();
solve();
return ;
}
 

ural 2071. Juice Cocktails的更多相关文章

  1. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  2. 【BZOJ-4435】Juice Junctions 最小割树(分治+最小割)+Hash

    4435: [Cerc2015]Juice Junctions Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 20  Solved: 11[Submi ...

  3. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  4. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  5. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  6. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

  7. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

  8. ural 2066. Simple Expression

    2066. Simple Expression Time limit: 1.0 secondMemory limit: 64 MB You probably know that Alex is a v ...

  9. ural 2065. Different Sums

    2065. Different Sums Time limit: 1.0 secondMemory limit: 64 MB Alex is a very serious mathematician ...

随机推荐

  1. c语言强制类型转换

    一.强制类型转换 printf("3/2+100.5=%f",3/2+100.5);//100.5错误表达 printf ("    (float)3/(float)2 ...

  2. Git - 使用指南

    GIT (分布式版本控制系统) 编辑 Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.[1]  Git的读音为/gɪt/. Git是一个开源的分布式版本控制系统,可以 ...

  3. drozer unknown module处理办法

    将目录切换到drozer安装目录,然后在执行:

  4. jQuery each用法及each解析json

    $(function(){ $("button").click( function(){ var a1=""; var a2=""; var ...

  5. 设计模式学习之组合模式(Composite,结构型模式)(10)

    转载地址:http://www.cnblogs.com/zhili/p/CompositePattern.html 一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系 ...

  6. 【PHP Cookie&&Session】

    大部分的人都知道Cookie,但是可能不了解Session,现在对这两者进行解释. 问题的提出: 有些网站会提示用户在一定的时间之内免登陆,这是用的什么技术?答案是Cookie技术. 有些购物网站会提 ...

  7. 攻城狮在路上(肆)How tomcat works(二) 一个简单的servlet容器

    该节在上一节的基础上增加了所谓对静态资源和动态资源访问的不同控制流程.示例里面采用的是对路径“/servlet/”进行了特殊处理. 一. 主要还是从HttpServer1中的main方法开始,先解析出 ...

  8. 攻城狮在路上(叁)Linux(十二)--- Linux的目录与路径

    一.相对路径与绝对路径: A.绝对路径:由根目录/开始写起的路径,例如 /usr/share/doc B.相对路径:不是由根目录/开始写起的路径. 二.目录的相关操作: 1.cd: 目录切换 cd ~ ...

  9. Java后端WebSocket的Tomcat实现

    转自:http://blog.chenzuhuang.com/archive/28.html 文章摘要随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5 ...

  10. UED

    User Experience Design(用户体验设计),简称UED.UED是进行产品策划的主力之一,他们用自己的知识.经验.设计能力拿出设计方案. UED不只是互联网专家,还是行业专家.能够用自 ...