P2870 [USACO07DEC]Best Cow Line G
P2870 [USACO07DEC]Best Cow Line G
题意:
给你一个字符串,每次从首或尾取一个字符组成字符串,问所有能够组成的字符串中字典序最小的一个。
题解:
现在要组成字典序最小的,那我们每次就尽可能取小的
我们从两端开始,如果有一端小,那肯定选小的一段。如果两端一样大,此时我们要考虑这两端后面的影响,我们要尽可能选后面较小的,比如图中蓝色一样,但是紫色比橙色小,那我们两端优先取右侧蓝色,因为这样可以保证后面取得小
那么如何比较两端这个大小,我们可以将字符串翻转一倍,这样就相当于比两个后缀的rank,跑个后缀数组就行
代码:
// Problem: P2870 [USACO07DEC]Best Cow Line G
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P2870
// Memory Limit: 250 MB
// Time Limit: 1500 ms
// Data:2021-08-22 11:41:01
// By Jozky#include <bits/stdc++.h>
#include <unordered_map>
#define debug(a, b) printf("%s = %d\n", a, b);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
clock_t startTime, endTime;
//Fe~Jozky
const ll INF_ll= 1e18;
const int INF_int= 0x3f3f3f3f;
void read(){};
template <typename _Tp, typename... _Tps> void read(_Tp& x, _Tps&... Ar)
{x= 0;char c= getchar();bool flag= 0;while (c < '0' || c > '9')flag|= (c == '-'), c= getchar();while (c >= '0' && c <= '9')x= (x << 3) + (x << 1) + (c ^ 48), c= getchar();if (flag)x= -x;read(Ar...);
}
template <typename T> inline void write(T x)
{if (x < 0) {x= ~(x - 1);putchar('-');}if (x > 9)write(x / 10);putchar(x % 10 + '0');
}
void rd_test()
{#ifdef LOCALstartTime= clock();freopen("in.txt", "r", stdin);
#endif
}
void Time_test()
{#ifdef LOCALendTime= clock();printf("\nRun Time:%lfs\n", (double)(endTime - startTime) / CLOCKS_PER_SEC);
#endif
}
const int N= 1000010;char s[N];
int n, sa[N], rk[N], oldrk[N << 1], id[N], px[N], cnt[N];
char ans[N];
bool cmp(int x, int y, int w)
{return oldrk[x] == oldrk[y] && oldrk[x + w] == oldrk[y + w];
}int main()
{int i, m= 300, p, w;int t;read(t);//char ch= getchar();for (int i= 1; i <= t; i++) {scanf("%c", &s[i]);char ch= getchar();}
// for (int i= 1; i <= t; i++) {// printf("s[i]=%c\n", s[i]);
// }int len= t; for (int i= 1; i <= len; i++) {s[i + len]= s[len - i + 1];}
// for (int i= 1; i <= 2*t; i++) {// printf("s[i]=%c\n", s[i]);
// }n= 2*t;for (i= 1; i <= n; ++i)++cnt[rk[i]= s[i]];for (i= 1; i <= m; ++i)cnt[i]+= cnt[i - 1];for (i= n; i >= 1; --i)sa[cnt[rk[i]]--]= i;for (w= 1; w < n; w<<= 1, m= p) { // m=p 就是优化计数排序值域for (p= 0, i= n; i > n - w; --i)id[++p]= i;for (i= 1; i <= n; ++i)if (sa[i] > w)id[++p]= sa[i] - w;memset(cnt, 0, sizeof(cnt));for (i= 1; i <= n; ++i)++cnt[px[i]= rk[id[i]]];for (i= 1; i <= m; ++i)cnt[i]+= cnt[i - 1];for (i= n; i >= 1; --i)sa[cnt[px[i]]--]= id[i];memcpy(oldrk, rk, sizeof(rk));for (p= 0, i= 1; i <= n; ++i)rk[sa[i]]= cmp(sa[i], sa[i - 1], w) ? p : ++p;}
// for (int i= 1; i <= n; i++)
// printf("sa[i]=%d", sa[i]);int l= 1, r= t;int tot= 0;while (l <= r) {if (rk[l] < rk[len + (len - r) + 1])cout << s[l++];elsecout << s[r--];tot++;if (tot % 80 == 0)printf("\n");}return 0;
}
P2870 [USACO07DEC]Best Cow Line G相关推荐
- P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(加强版)(贪心+hash哈希)
P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(加强版)(贪心+hash哈希) 洛谷上这道水题丧心病狂地把数据范围加到了500000 普通的做法肯定A不了了, ...
- P3033 [USACO11NOV]Cow Steeplechase G
P3033 [USACO11NOV]Cow Steeplechase G 提交1.80k 通过638 时间限制1.00s 内存限制125.00MB 提交答案加入题单 复制题目 题目提供者FarmerJ ...
- 贪心 POJ - 3617 Best Cow Line
Best Cow Line POJ - 3617 FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of ...
- 【BZOJ】3301: [USACO2011 Feb] Cow Line(康托展开)
http://www.lydsy.com/JudgeOnline/problem.php?id=3301 其实这一题很早就a过了,但是那时候看题解写完也是似懂非懂的.... 听zyf神犇说是康托展开, ...
- POJ 3617 Best Cow Line 贪心算法
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 Descri ...
- POJ 3617 Best Cow Line(最佳奶牛队伍)
POJ 3617 Best Cow Line Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] FJ is about to t ...
- 专题突破之反悔贪心——建筑抢修,Cow Coupons G, Voting (Hard Version),Cardboard Box
文章目录 [JSOI2007]建筑抢修 [USACO12FEB]Cow Coupons G CF1251E2 Voting (Hard Version) CF436E Cardboard Box [J ...
- Cow Line(洛谷-P3014)
题目描述 The N (1 <= N <= 20) cows conveniently numbered 1...N are playing yet another one of thei ...
- 【BZOJ】【3301】【USACO2011 Feb】Cow Line
康托展开 裸的康托展开&逆康托展开 康托展开就是一种特殊的hash,且是可逆的-- 康托展开计算的是有多少种排列的字典序比这个小,所以编号应该+1:逆运算同理(-1). 序列->序号:( ...
最新文章
- 创建windows窗口并且获得窗口句柄
- IO设备在OS中的权衡
- linux内核cfs浅析
- python调用系统命令_Python如何调用外部系统命令
- php表单复选传值,jQuery+SpringMVC中的复选框选择与传值实例_jquery
- c语言编程经典实例利润,C语言经典编程实例100题解答
- c语言获取ip地址,如何用C语言获得本机IP地址
- vcpkg编译库位数总结
- A Game with Traps—— 二分
- 【光学设计基础】--01像差理论基础
- 国内外GIS基础软件对比分析优缺特性及实际工作生产应用和成功案例综合评价
- 5分钟使用Python爬取豆瓣TOP250电影榜
- vue报错Navigating to current location (/login) is not allowed
- 软考java题目_2016下半年软考程序员考试冲刺模拟试题及答案(三)
- Java实现word文档转换为pdf,jodconverter
- Windows上CLion配置和使用教程
- linux查看某个nas盘io,nas(网络附加存储)与san知识解释
- 安装虚拟光驱时提示windows无法访问指定设备、路径或文件。可能没有权限访问该项目
- 关于Visual studio 2015 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包的解决方案
- 王者荣耀服务器维修多久,王者荣耀服务器维护多久?苹果更新后进不去是正常的吗?...
热门文章
- 如何机智的弄坏一台电脑?
- 史上最硬核的数学老师!搞发明、造大炮,让战斗民族直叫爸爸,看完我跪了......
- 《悦趣式连锁反应》玩转STEM教育!529块积木元件,N+1款炫酷模型
- C语言入门经典材料领走不谢!
- 数学本是非常有趣的,只是教科书呈现的方式太死板
- HTML5中常见的列表标签包括,介绍几个常用的HTML5标签
- java基础代码下载_Java基础(一)(示例代码)
- tcl mysql_MySQL·TCL语言
- client中周期性边界_「微评」增加艺术品在投资组合中的比例 推进国家艺术软实力...
- mysql在线快速修改密码_MySQL修改密码的几种方式