P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold
我比赛的时候A了,luogu上25分,QAQ,又憨又傻的200+代码,我为什么要干电脑干的事情,无语了。
如果左边<右边,取左
如果右边<左边,取右
如果相等,就向中间找,直到找到第一个不同的,然后给电脑指明下一步是取队首或队尾取就行了。

AC代码:

 #include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.18
using namespace std;
int n;
int l,r;
int cnt;
char a[],b[];
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void deal(int &x,int &y)
{
int l=x,r=y;
while(a[l]==a[r]&&l<r)
l++,r--;
if(a[l]<a[r])
{
b[++cnt]=a[x];
x++;
}
else
{
b[++cnt]=a[y];
y--;
}
} int main()
{
in(n);
For(i,,n)
cin>>a[i];
l=,r=n;
while(l<=r)
{
if(a[l]<a[r])
{
b[++cnt]=a[l];
l++;
}
else
if(a[l]>a[r])
{
b[++cnt]=a[r];
r--;
}
else
{
if(l!=r)
deal(l,r);
else
{
b[++cnt]=a[r];
r--;
}
}
}
For(i,,cnt)
{
p(b[i]);
if(i%==)
p('\n');
}
return ;
}

憨代码:

 #include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.18
using namespace std;
int n;
int l,r;
int cnt;
char a[],b[];
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} void deal(int &x,int &y)
{
int l=x,r=y;
char now=a[l];
while(a[l]==a[r]&&l<r)
{
l++,r--;
/* if(a[l]==a[r]&&a[l]!=now)
now=a[l];*/
}
if(l==r)
{
if(a[l]<now)
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
}
else
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
while(y>r)
{
b[++cnt]=a[y];
y--;
}
}
}
else
if(a[l]>=now&&a[r]>=now)
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
while(y>r)
{
b[++cnt]=a[y];
y--;
}
}
else
if(a[l]<=now&&a[r]<=now)
{
if(l-x>y-r)
{
while(y>r)
{
b[++cnt]=a[y];
y--;
}
}
else
if(l-x<y-r)
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
}
else
if(l-x==y-r)
{
if(a[l]>a[r])
{
while(y>r)
{
b[++cnt]=a[y];
y--;
}
}
else
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
}
}
}
else
if(a[l]>=now&&now>=a[r])
{
while(y>r)
{
b[++cnt]=a[y];
y--;
}
}
else
if(a[l]<=now&&now>=a[r])
{
while(x<l)
{
b[++cnt]=a[x];
x++;
}
}
} int main()
{
in(n);
For(i,,n)
cin>>a[i];
l=,r=n;
while(l<=r)
{
if(a[l]<a[r])
{
b[++cnt]=a[l];
l++;
}
else
if(a[l]>a[r])
{
b[++cnt]=a[r];
r--;
}
else
{
if(l!=r)
deal(l,r);
else
{
b[++cnt]=a[r];
r--;
}
}
}
For(i,,cnt)
{
p(b[i]);
if(i%==)
p('\n');
}
return ;
}

P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold的更多相关文章

  1. P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 解题报告

    P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold 题意 给一个字符串,每次可以从两边中的一边取一个字符,要求取出的字符串字典序最小 可以Hash+二分 也可以S ...

  2. 洛谷P2870 - [USACO07DEC]最佳牛线Best Cow Line

    Portal Description 给出一个字符串\(s(|s|\leq3\times10^4)\),每次从\(s\)的开头或结尾取出一个字符接在新字符串\(s'\)的末尾.求字典序最小的\(s'\ ...

  3. 洛谷P2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

    思路大概和其他的题解一样: 从当前字符串最前面,最后面选一个字典序较小的然后拉到一个新的字符串序列中,如果相同就一直往中间扫描直到发现不同为止(一个字符如果被选中之后那么就不可以再次选择了),所以我们 ...

  4. luogu2870 [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold

    ref #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  5. [luoguP2870] [USACO07DEC]最佳牛线,黄金Best Cow Line, Gold(后缀数组)

    传送门 数据小的话贪心就行. 可以把这个串翻转再接到后面,再求后缀数组,求出 rank 数组就很简单了. ——代码 #include <cstdio> #include <iostr ...

  6. POJ3623 Best Cow Line, Gold 【后缀数组】

    最好的牛线,金 时间限制: 5000MS   内存限制: 65536K 提交总数: 5917   接受: 2048 描述 FJ即将把他的ñ(1≤ ñ ≤30,000)头牛竞争一年一度的"年度 ...

  7. POJ3623:Best Cow Line, Gold(后缀数组)

    Description FJ is about to take his N (1 ≤ N ≤ 30,000) cows to the annual"Farmer of the Year&qu ...

  8. HDU 3623 Best Cow Line, Gold(模拟,注意思路,简单)

    题目 POJ 3617 和 这道题题目一样,只是范围稍稍再小一点. //模拟试试 #include<stdio.h> #include<string.h> #include&l ...

  9. 【POJ 3623】 Best Cow Line, Gold (后缀数组)

    [题意] [分析] 后缀数组水题,嗯,不认真看输出像我一样就会被坑.. #include<cstdio> #include<cstdlib> #include<cstri ...

随机推荐

  1. 【bzoj2440】 中山市选2011—完全平方数

    http://www.lydsy.com/JudgeOnline/problem.php?id=2440 (题目链接) 题意 求第K个不含有完全平方因子的数 Solution 没想到莫比乌斯还可以用来 ...

  2. Nginx反向代理下IIS获取真实IP

    1. iis 如果放在反向代理后面,日志里的c-ip是反向代理服务器的ip,不是真正用户的ip,想要记录用户的ip要做两件事. 一.在反向代理设置X-Forwarded-For段,以下为nginx下的 ...

  3. hdu 1789 Doing HomeWork Again (贪心算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS ...

  4. 鸟哥的Linux私房菜——第十一章

    视频链接: 土豆:http://www.tudou.com/programs/view/yT0PfIWU720 B站(推荐): http://www.bilibili.com/video/av9877 ...

  5. JAVA-Servlet高级应用

    会话只是指一段指定的时间间隔. 会话跟踪是维护用户状态(数据)的一种方式.它也被称为servlet中的会话管理. Http协议是一个无状态的,所以我们需要使用会话跟踪技术来维护用户状态. 每次用户请求 ...

  6. bzoj千题计划273:bzoj4710: [Jsoi2011]分特产

    http://www.lydsy.com/JudgeOnline/problem.php?id=4710 答案=总方案数-不合法方案数 f[i][j] 前i种特产分给j个人(可能有人没有分到特产)的总 ...

  7. [转载]Supporting OData $inlinecount with the new Web API OData preview package

    http://www.strathweb.com/2012/08/supporting-odata-inlinecount-with-the-new-web-api-odata-preview-pac ...

  8. opacity设定图片透明度

    实例 1 - 创建透明图像 定义透明效果的 CSS3 属性是 opacity. 首先,我们将展示如何通过 CSS 来创建透明图像. 常规图像: 带有透明度的相同图像: 请看下面的 CSS: img { ...

  9. HDU 2056 龟兔赛跑 (DP)

    题意:见题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 解题报告:以前一直没看出来这题是个DP题,知道是DP题就简单了 .首先要把起点和终点看成 ...

  10. IL反编译的实用工具Ildasm.exe

    初识Ildasm.exe——IL反编译的实用工具    https://www.cnblogs.com/yangmingming/archive/2010/02/03/1662307.html   学 ...