zoukankan      html  css  js  c++  java
  • HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)

    Problem Description
    In this problem, you are given several strings that contain only digits from '0' to '9', inclusive.
    An example is shown below.
    101
    123
    The set S of strings is consists of the N strings given in the input file, and all the possible substrings of each one of them.
    It's boring to manipulate strings, so you decide to convert strings in S into integers.
    You can convert a string that contains only digits into a decimal integer, for example, you can convert "101" into 101, "01" into 1, et al.
    If an integer occurs multiple times, you only keep one of them. 
    For example, in the example shown above, all the integers are 1, 10, 101, 2, 3, 12, 23, 123.
    Your task is to calculate the remainder of the sum of all the integers you get divided by 2012.
     
    Input
    There are no more than 20 test cases.
    The test case starts by a line contains an positive integer N.
    Next N lines each contains a string consists of one or more digits.
    It's guaranteed that 1≤N≤10000 and the sum of the length of all the strings ≤100000.
    The input is terminated by EOF.
     
    Output
    An integer between 0 and 2011, inclusive, for each test case.
     
    题目大意:计算n个串的所有子串(重复的算一个)的和,结果求余2012。
    思路:把n个字符串用一个奇怪的东东(比如10)连起来,建一个后缀自动机。
    由于在后缀自动机上,每一条路径到一个点,都唯一代表着一个字符串,所以重复什么的就没有了。
    然后后缀自动机是一个DAG,随便搞搞就能做了。
    PS:对后缀自动机熟悉的话,这题还是挺水的,是当时后缀自动机还不普及的缘故么。
     
    代码(406MS):
     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 using namespace std;
     5 
     6 const int MAXN = 110000 + 10;
     7 const int MOD = 2012;
     8 char buf[MAXN];
     9 struct State {
    10     State *fail, *go[11];
    11     int val, dp, cnt;
    12     bool mark;
    13     /*
    14     State() :
    15             fail(0), val(0) {
    16         memset(go, 0, sizeof go);
    17     }*/
    18 }*root, *last;
    19 State statePool[MAXN * 2], *cur;
    20 
    21 void init() {
    22     memset(statePool, 0, sizeof(statePool));
    23     cur = statePool;
    24     root = last = cur++;
    25 }
    26 
    27 void extend(int w) {
    28     State *p = last, *np = cur++;
    29     np->val = p->val + 1;
    30     while (p && !p->go[w])
    31         p->go[w] = np, p = p->fail;
    32     if (!p) np->fail = root;
    33     else {
    34         State*q = p->go[w];
    35         if (p->val + 1 == q->val) np->fail = q;
    36         else {
    37             State *nq = cur++;
    38             memcpy(nq->go, q->go, sizeof q->go);
    39             nq->val = p->val + 1;
    40             nq->fail = q->fail;
    41             q->fail = nq;
    42             np->fail = nq;
    43             while (p && p->go[w] == q)
    44                 p->go[w] = nq, p = p->fail;
    45         }
    46     }
    47     last = np;
    48 }
    49 
    50 inline void update_add(int &a, const int &b) {
    51     a = (a + b) % MOD;
    52 }
    53 
    54 struct Node {
    55     State *p;
    56     bool operator < (const Node &rhs) const {
    57         return p->val < rhs.p->val;
    58     }
    59 } a[MAXN * 2];
    60 
    61 int main() {
    62     int n;
    63     while(scanf("%d", &n) != EOF) {
    64         init();
    65         for(int i = 1; i <= n; ++i) {
    66             scanf("%s", buf);
    67             for(char *pt = buf; *pt; ++pt)
    68                 extend(*pt - '0');
    69             extend(10);
    70         }
    71         int m = 0, ans = 0;
    72         for(State *p = statePool; p != cur; ++p) a[m++].p = p;
    73         sort(a, a + m);
    74         root->cnt = 1;
    75         for(int i = 0; i < m; ++i) {
    76             State *pt = a[i].p;
    77             if(pt == root->go[0] || pt->mark) continue;
    78             update_add(ans, pt->dp);
    79             for(int j = 0; j < 10; ++j) {
    80                 if(!pt->go[j]) continue;
    81                 update_add(pt->go[j]->dp, 10 * pt->dp + pt->cnt * j);
    82                 update_add(pt->go[j]->cnt, pt->cnt);
    83             }
    84             if(pt->go[10]) pt->go[10]->mark = true;
    85         }
    86         printf("%d
    ", ans);
    87     }
    88 }
    View Code
  • 相关阅读:
    CSS3 页面跳转的动画效果
    JS/React 判断对象是否为空对象
    React 根据官方总结的规范
    ckeditor字数限制
    swfobject.js IE兼容问题
    Jcrop 做图片剪裁 在IE中无法显示问题解决办法
    WebApp 中用 hashchange 做路由解析
    全国省市区Json文件 ,做省市区联动很轻松
    解决用友U8删除用户时提示“用户已启用”不能删除的问题
    CFUpdate高速模式下出现Error #2038提示的解决方案
  • 原文地址:https://www.cnblogs.com/oyking/p/3354734.html
Copyright © 2011-2022 走看看