[codeforces-315D div2]模拟

题目:给两个字符串a、b,问从a中删去若干字符后最多可以得到多少个b串的重复串(bb...b的形式,b的长度不超过100),其中a串是由一个长度不超过100的字符串s重复k次得到的

思路: 暴力匹配a和b,由于s,b的长度都不超过100,标记每次匹配后a串指针的位置对len(s)的模,那么最多有100种标记,每种标记最多导致a串指针移动100*100位,那么在a串的前1e6个字符,一定可以得到重复的标记,而重复的标记之间就是循环节,跳过中间的若干循环节,处理最后剩余的a串字符(一定小于1e6个),继续暴力匹配下就行了。


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

/* ******************************************************************************** */

#include <iostream>                                                                 //

#include <cstdio>                                                                   //

#include <cmath>                                                                    //

#include <cstdlib>                                                                  //

#include <cstring>                                                                  //

#include <vector>                                                                   //

#include <ctime>                                                                    //

#include <deque>                                                                    //

#include <queue>                                                                    //

#include <algorithm>                                                                //

#include <map>                                                                      //

using namespace std;                                                                //

                                                                                    //

#define pb push_back                                                                //

#define mp make_pair                                                                //

#define X first                                                                     //

#define Y second                                                                    //

#define all(a) (a).begin(), (a).end()                                               //

#define foreach(a, i) for (typeof(a.begin()) i = a.begin(); i != a.end(); ++ i)     //

#define fill(a, x) memset(a, x, sizeof(a))                                          //

                                                                                    //

void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}    //

void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>                    //

void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;          //

while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>      //

void print(const T t){cout<<t<<endl;}template<typename F,typename...R>              //

void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>   //

void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}   //

                                                                                    //

typedef pair<intint> pii;                                                         //

typedef long long ll;                                                               //

typedef unsigned long long ull;                                                     //

                                                                                    //

template<typename T>bool umax(T&a, const T&b){return b>a?false:(a=b,true);}         //

template<typename T>bool umin(T&a, const T&b){return b<a?false:(a=b,true);}         //

template<typename T>                                                                //

void V2A(T a[],const vector<T>&b){for(int i=0;i<b.size();i++)a[i]=b[i];}            //

template<typename T>                                                                //

void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];}            //

                                                                                    //

/* -------------------------------------------------------------------------------- */

                                                                                    //

const int maxn = 1234567;

char s[maxn], a[123], c[123];

int lena, lenc, b, d, ta;

int buf[123], mark[123];

void init() {

    char *ps = s;

    for (int i = 0; i < b; i ++) {

        for (int j = 0; j < lena; j ++) {

            *ps ++ = a[j];

            if (ps - s == ta || ps - s > 1111111) return;

        }

    }

}

int work(int);

void solve(int &cc, int pp) {

    if (pp == ta - 1) return ;

    int rest = ta - pp - 1, cnt = rest / lena;

    if (rest % lena) cnt ++;

    ta = cnt * lena;

    s[ta] = 0;

    cc += work(pp % lena + 1);

}

int work(int start) {

    memset(mark, 0, sizeof(mark));

    memset(buf, 0, sizeof(buf));

    int cc = 0, nowc = 0;

    for (int i = start; s[i]; i ++) {

        if (s[i] == c[nowc]) nowc ++;

        if (nowc == lenc) {

            cc ++;

            nowc = 0;

            if (mark[i % lena]) {

                cc += (ta - i - 1) / (i + 1 - mark[i % lena]) * (cc - buf[i % lena]);

                int pp = i + (ta - i - 1) / (i + 1 - mark[i % lena]) *

                            (i + 1 - mark[i % lena]);

                solve(cc, pp);

                return cc;

            }

            else {

                mark[i % lena] = i + 1;

                buf[i % lena] = cc;

            }

        }

    }

    return cc;

}

int main() {

#ifndef ONLINE_JUDGE

    freopen("in.txt""r", stdin);

#endif // ONLINE_JUDGE

    cin >> b >> d;

    scanf("%s%s", a, c);

    lena = strlen(a);

    lenc = strlen(c);

    ta = lena * b;

    init();

    cout << work(0) / d << endl;

    return 0;                                                                       //

}                                                                                   //

                                                                                    //

                                                                                    //

                                                                                    //

/* ******************************************************************************** */

时间: 2024-08-03 02:56:41

[codeforces-315D div2]模拟的相关文章

Codeforces #180 div2 C Parity Game

// Codeforces #180 div2 C Parity Game // // 这道题的题目意思就不解释了 // // 题目有那么一点难(对于我而言),不多说啦 // // 解题思路: // // 首先如果a串和b串相等,不多说直接YES // 如果b串全是0,直接YES // 注意到a串有一个性质,1的个数不会超过本身的加1. // a有个1的上限设为x,b有个1的个数设为y,则如果x < y // 那么直接NO. // // 现在一般情况下,就是模拟啦,找到a的后缀和b的前缀一样的

Codeforces 583 DIV2 Robot&#39;s Task 贪心

原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使得总花费最小. 题解: 就傻傻的走就好..从左走到右,再走回来,更新序列和答案就好. 代码: #include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define MA

Codeforces #246(div2)

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <ti

Codeforces #245(div2)

A:A. Points and Segments (easy) 题目看了n久,开始觉得尼玛这是div2的题目么,题目还标明了easy.. 意思是给你一n个点,m个区间,在n个点上放蓝球或者红球,然后让你找一种选择方案使得m个区间内的蓝球和红球数量之差不超过1. 开始想过用dfs,不过这只是div2的A题而已.. 然后想了下,直接输出010101序列不就可以么. 交了一发,发现要先排个序,再输出就可以了. AC代码: #include<iostream> #include<cstdio&g

codeforces#327 div2

codeforces#327 div2 这场状态不好有点可惜,题目都不难,而且很好.. A题:水题. #include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; typedef lo

codeforces#FF(div2) DZY Loves Sequences

n个数,可以任意改变其中一个数,求最长的上升子区间长度 思路:记录一个from[i]表示从位置i的数开始最长的上升区间长度 记录一个to[i]表示到位置i的数所能达到的最长上升区间长度 枚举要改变的数的位置i,此时能达到的长度为to[i - 1] + from[i + 1] + 1,取最大值 //#pragma comment(linker, "/STACK:102400000,102400000") //HEAD #include <cstdio> #include &l

Codeforces 258 Div2

A题,n*m根木棍,相交放置,轮流取走相交的两根,最后谁不能行动,则输掉. min(n,m)&1 为1则先取者赢. B题,给定一个长度为n,且各不相同的数组,问能否通过交换连续一段L....R使得变成单调递增. 如果一开始就是递增的,那么直接输出L...R就是1 1,交换一个就行了:否则判断中间是否有且一段单调递减,且两端交换后使得数组递增. 代码: 1 //Template updates date: 20140718 2 #include <iostream> 3 #include

Codeforces 583 DIV2 Asphalting Roads 模拟

原题链接:http://codeforces.com/problemset/problem/583/A 题意: 很迷很迷,表示没看懂..但是你看样例就秒懂了 题解: 照着样例模拟就好 代码: #include<iostream> #include<cstring> #include<algorithm> #define MAX_N 55 using namespace std; bool h[MAX_N],v[MAX_N]; int n; int main(){ cin

codeforces 591B Rebranding (模拟)

Rebranding Problem Description The name of one small but proud corporation consists of n lowercase English letters. The Corporation has decided to try rebranding - an active marketing strategy, that includes a set of measures to change either the bra

Codeforces #263 div2 解题报告

比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注册的时候很想好好的做一下,但是网上喝了个小酒之后,也就迷迷糊糊地看了题目,做了几题,一觉醒来发现rating掉了很多,那个心痛啊! 不过,后来认真的读了题目,发现这次的div2并不是很难! 官方题解:http://codeforces.com/blog/entry/13568 A. Appleman and Easy Task 解析: 一个水题,判断每个细胞周围是否都是有偶数个相邻细胞.   代码