cf- 297 < b > -- 区间翻转操作的优化
2015-03-27 15:16
99 查看
B. Pasha and String
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.
Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|.
You face the following task: determine what Pasha's string will look like after m days.
Input
The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters.
The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string.
The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day.
Output
In the first line of the output print what Pasha's string s will look like after m days.
Sample test(s)
input
output
input
output
input
output
*****************************************************
题目简述:
给一串字符,给m步操作,每步操作是将从字符串的第n个开始到第 lenth-n+1 的区间进行翻转;求最后的字符串;
*******************************************************
本题暴力,用一个数组记录该字符背反转的次数,被翻转偶数次就相当与没有翻转,基数此就相当于和对位的字符进行一次交换;
刚开始我是 n^2 的算法,从第n个到第 lenth - n -1 的区间扫一遍加一,这样梅雨哦必要,因为后面一个翻转的次数总是大于等于前面一个的翻转次数,所以只由前一个累加过来就得到翻转次数;
首先扫一遍操作数组(先排个序),将区间的首尾位置标记出来,(就是加一),区间首之间的元素翻转次数是和前一个区间首次数相同的,然后来扫一遍数组,将各个元素的翻转次数累加出来,前后是堆成的,所以是要扫一半就可以了,同时处理前后两个对应的节点;
最后输出的时候检验此数,偶数鸳鸯输出,奇数输出对位元素;
代码如下:
View Code
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the string are numbered from 1 to |s| from left to right, where |s| is the length of the given string.
Pasha didn't like his present very much so he decided to change it. After his birthday Pasha spent m days performing the following transformations on his string — each day he chose integer ai and reversed a piece of string (a segment) from position ai to position |s| - ai + 1. It is guaranteed that 2·ai ≤ |s|.
You face the following task: determine what Pasha's string will look like after m days.
Input
The first line of the input contains Pasha's string s of length from 2 to 2·105 characters, consisting of lowercase Latin letters.
The second line contains a single integer m (1 ≤ m ≤ 105) — the number of days when Pasha changed his string.
The third line contains m space-separated elements ai (1 ≤ ai; 2·ai ≤ |s|) — the position from which Pasha started transforming the string on the i-th day.
Output
In the first line of the output print what Pasha's string s will look like after m days.
Sample test(s)
input
abcdef 1 2
output
aedcbf
input
vwxyz 2 2 2
output
vwxyz
input
abcdef 3 1 2 3
output
fbdcea
*****************************************************
题目简述:
给一串字符,给m步操作,每步操作是将从字符串的第n个开始到第 lenth-n+1 的区间进行翻转;求最后的字符串;
*******************************************************
本题暴力,用一个数组记录该字符背反转的次数,被翻转偶数次就相当与没有翻转,基数此就相当于和对位的字符进行一次交换;
刚开始我是 n^2 的算法,从第n个到第 lenth - n -1 的区间扫一遍加一,这样梅雨哦必要,因为后面一个翻转的次数总是大于等于前面一个的翻转次数,所以只由前一个累加过来就得到翻转次数;
首先扫一遍操作数组(先排个序),将区间的首尾位置标记出来,(就是加一),区间首之间的元素翻转次数是和前一个区间首次数相同的,然后来扫一遍数组,将各个元素的翻转次数累加出来,前后是堆成的,所以是要扫一半就可以了,同时处理前后两个对应的节点;
最后输出的时候检验此数,偶数鸳鸯输出,奇数输出对位元素;
代码如下:
#include<cstdio> #include<iostream> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<string> #include<vector> using namespace std; char s[200010]; int m,sl=0,a[200010]={0},d[100010]={0}; int main() { memset(a,0,sizeof(a)); scanf("%s",s); scanf("%d",&m); sl=strlen(s); for(int i=0;i<m;i++) { scanf("%d",&d[i]); } sort(d,d+m); for(int i=0;i<m;i++) { a[d[i]-1]++; a[sl-d[i]]++; } for(int i=1;i<sl/2;i++) { a[i]+=a[i-1]; a[sl-i-1]+=a[sl-i]; } for(int i=0;i<sl;i++) { if(a[i]%2==0) printf("%c",s[i]); else printf("%c",s[sl-i-1]); } printf("\n"); return 0; }
View Code
相关文章推荐
- cf- 297 < a >--字符串操作技巧
- oracle 性能优化操作五:尽量去掉 "<>"
- oracle 性能优化操作五:尽量去掉 "<>"
- c文件操作<3>
- Android中View绘制优化二一---- 使用<include />标签复用布局文件
- <Win32 API> 文件操作CreateFile/ReadFile/WriteFile
- 无法跨越程序集边界使用程序集“DataCheck, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中的类型“List<ILayer>”,因为该类型有一个为嵌入互操作类型的泛型类型参数
- 性能优化-列表类型转换(ConvertList<TSource, TResult>)
- Android UI 优化 使用<include/>和 <merge />标签
- 性能优化:使用SparseArray代替HashMap<Integer,Object>
- 参考<<精通正则表达式>>一书对我们ETL log解析模块一个正则的优化
- Java 移位操作>>,<<,>>> 位逻辑运算符 &, |,~,^
- 一步步优化JVM<五>:优化延迟或者响应时间(3)
- Python脚本控制的WebDriver 常用操作 <二十> 处理表单元素
- jstl操作数据库<sql:setDataSource/>、<sql:query/>
- Android UI 优化 使用<include/>和 <merge />标签
- Python脚本控制的WebDriver 常用操作 <二十八> 超时设置和cookie操作
- 关于优化性能<主要是速度方面>的个人心得 【转】
- JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-003映射实体时的可选操作(<delimited-identifiers/>、PhysicalNamingStrategy、PhysicalNamingStrategyStandardImpl、、、)