所有划分均合法:{453},{45,3},{4,53},{4,5,3}。
合法方案为:{33,33},{33,3,3},{3,33,3},{3,3,33},{3,3,3,3}。
To solve the problem of determining the number of ways to partition the given string s into non-empty, contiguous, and non-overlapping substrings where each substring is "interesting" (i.e., no two different segments within the substring of length greater than 1 have the same sum of digits), we can leverage dynamic programming with specific optimizations.
Approach Overview
-
Dynamic Programming (DP) Setup:
- Define
dp[i] as the number of valid ways to partition the first i characters of the string.
- Initialize
dp[0] = 1 since there's one way to partition an empty string.
-
Substring Length Constraint:
- From the problem analysis, it's deduced that any substring longer than 19 characters cannot be "interesting" due to the pigeonhole principle. Hence, we only need to consider substrings of length up to 19.
-
Efficiently Checking "Interesting" Substrings:
- For each position
i in the string, consider all possible substrings starting at i with lengths from 1 to 19.
- For each substring
s[i..j], ensure that all possible sums of its internal substrings of length ≥2 are unique.
- Utilize a bitmask (since the maximum possible sum for a substring of length 19 with digits
0-9 is 171) to track seen sums efficiently.
-
Updating the DP Table:
- If a substring
s[i..j] is "interesting", update dp[j] by adding dp[i] to it.
- Ensure all computations are performed modulo
998244353 to handle large numbers.
-
Final Answer:
- The value
dp[n] (where n is the length of the string) will give the total number of valid partitioning ways.
Implementation Details
Given the large potential size of the input string (up to 15,000,000 characters), the implementation needs to be highly optimized. Below is a C++ implementation that adheres to these optimizations:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 998244353;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
int n = s.size();
// Initialize DP array
// To save space, use a rolling array (current and next)
// But since we need to access dp[i], it's better to use a 1D array
// However, with n=1.5e7, it's too large for the stack, so use dynamic allocation
// Alternatively, use prefix sum dp
// Use a vector of unsigned int to save space
// Initialize with 0
// Use modulo addition
// To optimize memory, use 4-byte integers
// Allocate dp as a vector of unsigned int
// It's manageable with n=1.5e7 (~60MB)
vector<int> dp(n+1, 0);
dp[0] =1;
// Precompute prefix sums for quick substring sum computation
// Since we process substrings up to length 19, we can compute sums on the fly
for(int i=0; i<n; ++i){
if(dp[i]==0) continue;
// Initialize bitmask for sums
// Max sum is 171, so use 6 uint32_t (32*6=192 bits)
// Alternatively, use a std::bitset<172>
// But for speed, use an integer array
unsigned int seen =0; // Bitmask for sums 0 to 171
int current_sum =0;
// To store the sum for each substring ending at current position
// Iterate up to 19 characters
for(int l=1; l<=19 && i+l <=n; ++l){
current_sum += (s[i+l-1]-'0');
if(l>=2){
if(seen & (1U << current_sum)){
// This substring is not "interesting", break
break;
}
seen |= (1U << current_sum);
}
// Update dp
dp[i+l] = (dp[i+l] + dp[i]) % MOD;
}
}
cout << dp[n];
}
Explanation of the Code
-
Input Handling:
- The input string
s is read from standard input.
-
Dynamic Programming Array Initialization:
- A DP array
dp of size n+1 (where n is the length of s) is initialized to store the number of valid partition ways up to each index.
dp[0] is set to 1 as the base case.
-
Processing Each Position:
- Iterate through each position
i in the string.
- For each position, if
dp[i] is greater than 0, attempt to extend the partition by considering substrings of lengths 1 to 19.
- For each possible substring length:
- Update the current sum by adding the next digit.
- For substrings of length
≥2, check if the sum has been seen before using a bitmask.
- If a duplicate sum is found, the substring cannot be "interesting," and the loop breaks early.
- If the substring remains "interesting," update the
dp array accordingly.
-
Bitmask Strategy:
- A single
unsigned int is used as a bitmask to track the sums of substrings efficiently.
- Each bit in the bitmask represents whether a particular sum has been encountered.
-
Final Output:
- After processing all positions, the value
dp[n] contains the total number of valid partitioning ways.
- The result is printed modulo
998244353 as required.
Optimizations Implemented
-
Bitmask Usage: Utilizing a bitmask to track seen sums reduces the time complexity significantly by allowing constant-time checks for duplicates.
-
Early Termination: Breaking out of the loop as soon as a duplicate sum is detected prevents unnecessary computations.
-
Space Efficiency: Using a 1D DP array with int types ensures memory usage is kept within acceptable limits even for large input sizes.
Conclusion
This solution efficiently computes the number of valid ways to partition the string while adhering to the constraints of the problem. By leveraging dynamic programming and bitmask optimizations, it handles large inputs within feasible time and space limits.