HDU 4436 str2int (12 years in Tianjin, SAM)

Source: Internet
Author: User

Like SA, add special characters between strings. Here, use the number 10 to create a SAM.

Then perform a topology operation on the SAM.

Record the number of paths to the node cnt, and the sum that previously reached the node.

The sum of the child nodes is added. The sum of the current node and * 10 + cnt * j indicate the number added at the end.

In the topology section, you can use the classic linear or brute force Len sorting.

But there are some notes:

1. 10 is a special character to be added, and the side of 10 is not transferred. This is obvious, except that all strings are combined in order not to calculate duplicate substrings.

2. Leading 0 should be ignored, that is, when transferring from root, it cannot go 0, otherwise it will be repeated.


[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <map>
# Include <cstring>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <set>
# Include <string>
# Include <queue>
# Define inf 100000005
# Define M 40
# Define N 210005
# Define maxn300005
# Define eps 1e-10
# Define zero (a) fabs (a) <eps
# Define Min (a, B) (a) <(B )? (A) (B ))
# Define Max (a, B) (a)> (B )? (A) (B ))
# Define pb (a) push_back ()
# Define mp (a, B) make_pair (a, B)
# Define mem (a, B) memset (a, B, sizeof ())
# Define LL unsigned long
# Define MOD 2012.
# Define lson step <1
# Define rson step <1 | 1
# Define sqr (a) (double) (a) * ())
# Define Key_value ch [ch [root] [1] [0]
# Define test puts ("OK ");
# Pragma comment (linker, "/STACK: 1024000000,1024000000 ")
Using namespace std;
Struct SAM
{
SAM * pre, * son [11];
Int len, cnt, sum;
} * Root, * tail, que [N], * B [N];
Int tot;
Char str [N/2];
Void add (int c, int l)
{
SAM * p = tail, * np = & que [tot ++];
Np-> len = l;
While (p & p-> son [c] = NULL) p-> son [c] = np, p = p-> pre;
If (p = NULL) np-> pre = root;
Else
{
SAM * q = p-> son [c];
If (p-> len + 1 = q-> len) np-> pre = q;
Else
{
SAM * nq = & que [tot ++];
* Nq = * q;
Nq-> len = p-> len + 1;
Np-> pre = q-> pre = nq;
While (p & p-> son [c] = q) p-> son [c] = nq, p = p-> pre;
}
}
Tail = np;
}
Bool cmp (int x, int y)
{
Return que [x]. len <que [y]. len;
}
Int len, c [N];
Int slove ()
{
Mem (c, 0 );
For (int I = 0; I <tot; I ++) c [que [I]. len] ++;
For (int I = 1; I <len; I ++) c [I] + = c [I-1];
For (int I = 0; I <tot; I ++) B [-- c [que [I]. len] = & que [I];
Root-> cnt = 1;
Root-> sum = 0;
Int ans = 0;
For (int I = 0; I <tot; I ++)
{
SAM * p = B [I];
For (int j = 0; j <10; j ++)
{
If (I = 0 & j = 0) continue;
If (p-> son [j])
{
SAM * q = p-> son [j];
Q-> cnt = (q-> cnt + p-> cnt) % MOD;
Q-> sum = (q-> sum + p-> sum * 10 + p-> cnt * j) % MOD;
}
}
Ans = (ans + p-> sum) % MOD;
}
Return ans;
}
Int main ()
{
Int n;
While (scanf ("% d", & n )! = EOF)
{
Tot = 0;
Root = tail = & que [tot ++];
Len = 1;
While (n --)
{
Scanf ("% s", str );
For (int I = 0; str [I]; I ++) add (str [I]-'0', len ++ );
Add (10, len ++ );
}
Printf ("% d \ n", slove ());
For (int I = 0; I <tot; I ++) que [I]. sum = que [I]. cnt = 0, que [I]. pre = NULL, mem (que [I]. son, NULL );
}
Return 0;
}

# Include <iostream>
# Include <cstdio>
# Include <map>
# Include <cstring>
# Include <cmath>
# Include <vector>
# Include <algorithm>
# Include <set>
# Include <string>
# Include <queue>
# Define inf 100000005
# Define M 40
# Define N 210005
# Define maxn300005
# Define eps 1e-10
# Define zero (a) fabs (a) <eps
# Define Min (a, B) (a) <(B )? (A) (B ))
# Define Max (a, B) (a)> (B )? (A) (B ))
# Define pb (a) push_back ()
# Define mp (a, B) make_pair (a, B)
# Define mem (a, B) memset (a, B, sizeof ())
# Define LL unsigned long
# Define MOD 2012.
# Define lson step <1
# Define rson step <1 | 1
# Define sqr (a) (double) (a) * ())
# Define Key_value ch [ch [root] [1] [0]
# Define test puts ("OK ");
# Pragma comment (linker, "/STACK: 1024000000,1024000000 ")
Using namespace std;
Struct SAM
{
SAM * pre, * son [11];
Int len, cnt, sum;
} * Root, * tail, que [N], * B [N];
Int tot;
Char str [N/2];
Void add (int c, int l)
{
SAM * p = tail, * np = & que [tot ++];
Np-> len = l;
While (p & p-> son [c] = NULL) p-> son [c] = np, p = p-> pre;
If (p = NULL) np-> pre = root;
Else
{
SAM * q = p-> son [c];
If (p-> len + 1 = q-> len) np-> pre = q;
Else
{
SAM * nq = & que [tot ++];
* Nq = * q;
Nq-> len = p-> len + 1;
Np-> pre = q-> pre = nq;
While (p & p-> son [c] = q) p-> son [c] = nq, p = p-> pre;
}
}
Tail = np;
}
Bool cmp (int x, int y)
{
Return que [x]. len <que [y]. len;
}
Int len, c [N];
Int slove ()
{
Mem (c, 0 );
For (int I = 0; I <tot; I ++) c [que [I]. len] ++;
For (int I = 1; I <len; I ++) c [I] + = c [I-1];
For (int I = 0; I <tot; I ++) B [-- c [que [I]. len] = & que [I];
Root-> cnt = 1;
Root-> sum = 0;
Int ans = 0;
For (int I = 0; I <tot; I ++)
{
SAM * p = B [I];
For (int j = 0; j <10; j ++)
{
If (I = 0 & j = 0) continue;
If (p-> son [j])
{
SAM * q = p-> son [j];
Q-> cnt = (q-> cnt + p-> cnt) % MOD;
Q-> sum = (q-> sum + p-> sum * 10 + p-> cnt * j) % MOD;
}
}
Ans = (ans + p-> sum) % MOD;
}
Return ans;
}
Int main ()
{
Int n;
While (scanf ("% d", & n )! = EOF)
{
Tot = 0;
Root = tail = & que [tot ++];
Len = 1;
While (n --)
{
Scanf ("% s", str );
For (int I = 0; str [I]; I ++) add (str [I]-'0', len ++ );
Add (10, len ++); www.2cto.com
}
Printf ("% d \ n", slove ());
For (int I = 0; I <tot; I ++) que [I]. sum = que [I]. cnt = 0, que [I]. pre = NULL, mem (que [I]. son, NULL );
}
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.