↓ Skip to main content
  1. Posts/

codeforces edu1 B Queries on a String

·531 words·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题意:给一个字符串(1E4),然后给m次操作(m<=300),每次操作是给定一个区间l,r,然后进行k次(k<=1E6)cyclic shift (rotation) 变换。

One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right.

For example, if the string s is abacaba and the query is _l_1 = 3, _r_1 = 6, _k_1 = 1 then the answer is abbacaa. If after that we would process the query _l_2 = 1, _r_2 = 4, _k_2 = 2 then we would get the string baabcaa.

简单的说。。就是把最后一位移动到第一位,其他位依次往后。

很容易想到,对于一个长度为len的字符串。进行i次变换和进行(i+len)次变换是等价的。

然后直接暴力搞。

offset为偏移量。对于位置i的偏移量为(i-l+len-k)%len,len为每次操作区间的长度。

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月04日 星期五 14时24分56秒
 4File Name :code/cf/edu/B.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25
26
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=1E4+7;
34
35char st[N];
36int len;
37int m;
38int l,r,k;
39int main()
40{
41	#ifndef  ONLINE_JUDGE
42	freopen("code/in.txt","r",stdin);
43  #endif
44
45	scanf("%s",st);
46	scanf("%d",&m);
47	while (m--)
48	{
49	    scanf("%d %d %d",&l,&r,&k);
50	    l--;
51	    r--; //强行让下标从0开始。。。处理起来方便
52	    int len = r-l+1;
53	    k = k % len;
54	    char tmp[N];
55
56	    for ( int i = l ; i <= r ; i++)
57	    {
58		int offset = (i-l+len-k)%len;
59		tmp[i] = st[offset+l];
60	    }
61	    for ( int i = l; i <= r ; i++) st[i] = tmp[i];
62
63//	    printf("%s\n",st);
64	}
65	printf("%s\n",st);
66
67
68  #ifndef ONLINE_JUDGE
69  fclose(stdin);
70  #endif
71    return 0;
72}

Related

(BC 一周年)hdu 5311 Hidden String

·767 words·2 mins
http://acm.hdu.edu.cn/showproblem.php?pid=5311 题意:问能否从一个给定的字符串中拿出三个不相交的字串(原串可以有剩余),组成字符串“anniversary” 思路:暴力。 比赛的时候没做出来,sad 我发现我有一个问题,就是不敢跑暴力 有不少题其实正解就是暴力 或者有的题,暴力不是标解,但是绝对可A,可我就不敢写… 就觉得不会是这样.. 说到底还是不自信吧…

codeforces #edu 1 A tricky sum

·493 words·1 min
题意:求1+2+..+n的和。。但是对于是2的整数幂的项数。。符号是-。。 思路:可以先当做正数。(n+1)*n/2; 然后减去二倍的2的整数次幂的项的和。 坑点: 妈蛋第三次了。。。我想求小于等于n的最大是2的几次幂。。。取整的时候用int又会迷之错误。。。为什么说是迷之错误。。因为我WA的点的数据拿下来在本地跑是没有问题的。。。一交上去就错。。。不明觉厉。。。下次遇到double类型是数一点要小心小心再小心。。。第一次遇到是pow的返回类型是double,然后答案莫名奇妙的差1.第二次是#334 div2 的A题。。一道傻逼算分数的题我WA了一个小时。。。第三次是这个。。向下取整不要用(int)的强制转换。。而用floor吧。。233

有了这个列表,程序员不愁没练手的小项目了

·6795 words·14 mins
我经常看有人发帖问关于项目点子的事,也看到了很多回帖,我自己也回了一些常见的项目。不过我觉得只列出三两个是远远不够的,因此就收集并整理了这个项目列表,大家要找简单的编程项目学习练手的话,可以收藏并扩散本文。这些项目并不是论文级别的,只是想抛砖引玉让大家能从中受些启发。

codeforces #334 div 2 C. Alternative Thinking

·517 words·2 mins
题意:给定一个01串。要进行一次变换:选一段连续的非空的字串,将这段串的0和1反转(0变成1,1变成0) 然后问能得到的最长的0,1交替的序列的长度是多少(不一定连续)