hdu 6103(Kirinriki)
题目链接:Kirinriki
题目描述:
找两个不重叠的字符串A,B。 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n-1-i} \right |\)。求最长的字符串长度。
思路:
官方题解,双指针维护。简单题。枚举对称中心。
在这里我给出我常用的双指针的写法。
int a[N];
int l=0,r=0,val=0;
while(r没有越界) //如果满足条件
{
if(val+a[r]<=key) // 加上 a[r] 是否满足条件?
{
val+=a[r];
r++;
更新最大值//满足条件的区间为 [l,r)
}
else //右移
{
val-=a[l];
l++
}
}
下面是枚举对称轴的写法:
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long int LL;
const int INF = 2e9 + 1e8;
const int MOD = 1e9 + 7;
const double eps = 0.0000000001;
void fre()
{
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
}
#define MSET(a, b) memset(a, b, sizeof(a))
const int maxn = 1e5 + 100;
char str[maxn];
int m;
int len, ans;
void nyist(int x,int y)
{
int dis=0,l=0,r=0;
while(y+r<len&&x-r>=0)
{
if(dis+abs(str[x-r]-str[y+r])<=m)
{
dis+=abs(str[x-r]-str[y+r]);
r++;
ans=max(ans,r-l);
}
else
{
dis-=abs(str[x-l]-str[y+l]);
l++;
}
}
}
int main()
{
int ncase;
scanf("%d", &ncase);
while (ncase--)
{
scanf("%d", &m);
ans = 0;
scanf("%s", str);
len = strlen(str);
for (int i = 0; i < len; i++)
{
nyist(i-1,i+1);
nyist(i,i+1);
}
printf("%d\n", ans);
}
return 0;
}
/**************************************************/
/** Copyright Notice **/
/** writer: wurong **/
/** school: nyist **/
/** blog : http://www.cnblogs.com/coded-ream/ **/
/**************************************************/
还有就是和枚举对称轴相反的写法;
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long int LL;
const int INF = 2e9 + 1e8;
const int MOD = 1e9 + 7;
const double eps = 0.0000000001;
void fre()
{
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
}
#define MSET(a, b) memset(a, b, sizeof(a))
const int maxn = 1e5 + 100;
char str[maxn];
int m;
int len, ans;
void nyist(int x,int y)
{
int dis=0,l=0,r=0;
while(x+r<y-r)
{
if(dis+abs(str[x+r]-str[y-r])<=m)
{
dis+=abs(str[x+r]-str[y-r]);
r++;
ans=max(ans,r-l);
}
else
{
dis-=abs(str[x+l]-str[y-l]);
l++;
}
}
}
int main()
{
int ncase;
scanf("%d", &ncase);
while (ncase--)
{
scanf("%d", &m);
ans = 0;
scanf("%s", str);
len = strlen(str);
len--;
for(int i=1;i<=len;i++) nyist(0,i);
for(int i=0;i<len;i++) nyist(i,len);
printf("%d\n", ans);
}
return 0;
}
/**************************************************/
/** Copyright Notice **/
/** writer: wurong **/
/** school: nyist **/
/** blog : http://www.cnblogs.com/coded-ream/ **/
/**************************************************/
hdu 6103(Kirinriki)的更多相关文章
- HDU 6103 Kirinriki(尺取法)
http://acm.hdu.edu.cn/showproblem.php?pid=6103 题意: 给出一个字符串,在其中找两串互不重叠的子串,计算它们之间的dis值,要求dis值小于等于m,求能选 ...
- HDU 6103 Kirinriki (思维 双指针)
Kirinriki Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 2017ACM暑期多校联合训练 - Team 6 1008 HDU 6103 Kirinriki (模拟 尺取法)
题目链接 Problem Description We define the distance of two strings A and B with same length n is disA,B= ...
- HDU - 6103 :Kirinriki(不错的尺取法)
We define the distance of two strings A and B with same length n is dis A,B =∑ i=0 n−1 |A i −B n−1−i ...
- hdu 6103 Kirinriki (枚举对称中心+双指针)
Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑(i=0 ...
- HDU 6103 17多校6 Kirinriki(双指针维护)
Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑i=0n− ...
- HDU 6103
题意: 求最长的两个不相交的子序列,dis <= m : 分析: 当时二分了答案,暴力匹配,TLE了,然后考虑了,O(n^2)预处理出所有区间 dis,然后答案是所有dis中>=m的最长长 ...
- hdu some problems in Multi-University Training Contest
hdu 6103 Kirinriki #include<bits/stdc++.h> using namespace std; int n,m,ans; ]; void doit(int ...
- 2017杭电多校第六场1008 Kirinriki
传送门 Kirinriki Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
随机推荐
- CPU_CState_PState and then ACPI on Wiki
http://wenku.baidu.com/link?url=eHbdT4EjdJx3dsQETGUIL8q1K3_EyuzGLWT0G103AEca0vs0gHR_v_3c0oaUL2gbkrr8 ...
- A charge WIFI point base on airbase-ng+dhcp+lamp+wiwiz
Make wifi as a hot point Make a script echo $0 $1 case $1 in "start") sleep 1 ifconfig wla ...
- ASP.NET动态网站制作(3)--css(2)
前言:css分为四次课讲完,第一节课内容见ASP.NET动态网站制作(2)--css(1),接下来的内容会涉及到定位.浮动.盒子模型(第二次课).css的具体应用(第三次课).css3(第四次课).今 ...
- 设置Eclipse中properties文件打开方式myeclipse一样有source和properties两个视图方法
东北大亨: 说明:如果想在eclipse的properties文件打开的方式出现source和properties视图就需要添加JBossTools插件 下面介绍如果添加插件: 1.打开官网 http ...
- JVM相关小结
对JVM中分层模型.垃圾回收期.垃圾回收算法趁着周末小结一下.有不对的地方,还请指正和讨论~ 1.JVM内存模型 2.JVM垃圾回收期 3.JVM垃圾回收算法 ------------------- ...
- 错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009)【转】
启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required by Tomcat v5.5 Server at localhost are alre ...
- Python:list、dict、string
<<List>>列表 [python] view plaincopy 创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 samp ...
- .NET Winform 将引用的dll文件集成到exe中(转)
Winform程序经常需要引用一些第三方dll文件,这些dll在发布后与exe文件保存在同一目录下,虽然将dll文件集成到exe中会增大文件尺寸,但程序目录会相对整洁. 下面介绍一种比较简单的集成方法 ...
- 九度OJ 1168:字符串的查找删除 (查找)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4276 解决:1699 题目描述: 给定一个短字符串(不含空格),再给定若干字符串,在这些字符串中删除所含有的短字符串. 输入: 输入只有1 ...
- 解决win7打印机共享出现“无法保存打印机设置(错误0x000006d9)的问题
最新解决win7打印机共享出现“无法保存打印机设置(错误0x000006d9)的问题,由系统下载吧率先分享: 有些用户在使用Windows7系统过程中,碰到到win7打印机共享出现“无法保存打印机设置 ...