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. 项目之solr全文搜索工具的安装

    1. Solr简介 Solr是一个基于Lucene的Java搜索引擎服务器.Solr 提供了层面搜索.命中醒目显示并且支持多种输出格式(包括 XML/XSLT 和 JSON 格式).它易于安装和配置, ...

  2. SOCKet 编程 简介

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  3. XMPP框架下微信项目总结(6)刷新好友列表(删除,添加好友)

    原理:1 服务器(openfire)添加/删除 好友,会向客户端(app)发送消息, 2 代理(xmppStreamDelegate)监听到添加/删除消息后,花名册模块(RosterModule)会在 ...

  4. python 连接sql server

    linux 下pymssql模块的安装 所需压缩包:pymssql-2.1.0.tar.bz2freetds-patched.tar.gz 安装: tar -xvf pymssql-2.1.0.tar ...

  5. 《C#本质论》读书笔记(12)委托和Lambda表达式

    12.1.委托概述 12.1.2 委托的数据类型 为了减少重复代码数量,可以将比较方法作为参数传递给 BubbleSort()方法.此外,为了将方法作为参数传递,必须有一个能够标识方法的数据类型--也 ...

  6. EasyUi – 1.入门

    1.页面引用. jquery,easyui,主题easyui.css,图标ico.css,语言zh_CN.js <script src="Scripts/jquery-easyui-1 ...

  7. 与你相遇好幸运,Waterline的多表关联

    >一对一关联 表示一个模型可能只与另一个模型关联.为了使模型知道它与其他哪些模型关联,外键必需包含在记录中.. http://imfly.github.io/sails-docs/concept ...

  8. OS Boot Loader -- 启动器

    这篇文章先抛出来,现在还没有彻底研究明白,但可以做个个人的小结和整理: 记得刚开始搞Linux的时候,普遍采用的是grub,后来有了grub2,尤其是在ubuntu那种非常差劲的不稳定的更新频繁的系统 ...

  9. sqlplus链接数据库报ORA-09925: Unable to create audit trail file

    [localhost.localdomain]:[/oracle11/app/oracle11/product/11.2.0/dbhome_1/dbs]$ sqlplus / as sysdba SQ ...

  10. [LeetCode] Min Stack

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...