zoukankan      html  css  js  c++  java
  • HDU 4436 str2int

    str2int

    Time Limit: 3000ms
    Memory Limit: 131072KB
    This problem will be judged on HDU. Original ID: 4436
    64-bit integer IO format: %I64d      Java class name: Main
    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.
     

    Sample Input

    5
    101
    123
    09
    000 
    1234567890

    Sample Output

    202

    Source

    解题:后缀自动机大法好
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int mod = 2012;
     4 const int maxn = 210001;
     5 struct node {
     6     int son[11],f,len;
     7     void init() {
     8         f = -1;
     9         len = 0;
    10         memset(son,-1,sizeof son);
    11     }
    12 };
    13 struct SAM {
    14     node e[maxn<<1];
    15     int tot,last;
    16     int newnode(int len = 0) {
    17         e[tot].init();
    18         e[tot].len = len;
    19         return tot++;
    20     }
    21     void init() {
    22         tot = last = 0;
    23         newnode(0);
    24     }
    25     void extend(int c) {
    26         int p = last,np = newnode(e[p].len + 1);
    27         while(p != -1 && e[p].son[c] == -1) {
    28             e[p].son[c] = np;
    29             p = e[p].f;
    30         }
    31         if(p == -1) e[np].f = 0;
    32         else{
    33             int q = e[p].son[c];
    34             if(e[p].len + 1 == e[q].len) e[np].f = q;
    35             else{
    36                 int nq = newnode();
    37                 e[nq] = e[q];
    38                 e[nq].len = e[p].len + 1;
    39                 e[np].f = e[q].f = nq;
    40                 while(p != -1 && e[p].son[c] == q){
    41                     e[p].son[c] = nq;
    42                     p = e[p].f;
    43                 }
    44             }
    45         }
    46         last = np;
    47     }
    48 }sam;
    49 char str[maxn];
    50 int cnt[maxn<<1],c[maxn<<1],sum[maxn<<1],sa[maxn<<1];
    51 int main() {
    52     int n,len;
    53     while(~scanf("%d",&n)){
    54         sam.init();
    55         for(int i = len = 0; i < n; ++i){
    56             scanf("%s",str + len);
    57             len = strlen(str);
    58             if(i + 1 < n) str[len++] = '0' + 10;
    59         }
    60         for(int i = 0; i < len; ++i)
    61             sam.extend(str[i] - '0');
    62         memset(c,0,sizeof c);
    63         memset(cnt,0,sizeof cnt);
    64         memset(sum,0,sizeof sum);
    65         for(int i = 0; i < sam.tot; ++i) c[sam.e[i].len]++;
    66         for(int i = 1; i <= len; ++i) c[i] += c[i-1];
    67         for(int i = sam.tot-1; i >= 0; --i) sa[--c[sam.e[i].len]] = i;
    68         int ret = 0;
    69         cnt[0] = 1;
    70         for(int i = 0; i < sam.tot; ++i){
    71             int x = sa[i];
    72             for(int j = 0; j < 10; ++j){
    73                 if(x == 0 && j == 0) continue;
    74                 cnt[sam.e[x].son[j]] += cnt[x];
    75                 sum[sam.e[x].son[j]] += sum[x]*10 + cnt[x]*j;
    76                 cnt[sam.e[x].son[j]] %= mod;
    77                 sum[sam.e[x].son[j]] %= mod;
    78             }
    79             ret = (ret + sum[x])%mod;
    80         }
    81         printf("%d
    ",ret);
    82     }
    83     return 0;
    84 }
    View Code
  • 相关阅读:
    nodejs 获取客户端 ip 地址
    如何使用 nvm-windows 管理 nodejs 版本
    redis 环境搭建
    利用 ssh 传输文件
    如何在 Centos7 中安装 gcc
    如何在 Centos7 中安装 nginx
    django迁移model到别的app中
    ssl生成证书
    pip安装mysql报错 ld: library not found for -lssl
    mac重置蓝牙模块
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4910888.html
Copyright © 2011-2022 走看看