Count the string(记忆化搜索)

本文介绍了一个关于字符串的算法问题,任务是计算一个字符串中所有非空前缀出现的次数总和,并通过模运算处理可能的大数值结果。文章提供了一种使用记忆化搜索的方法来解决该问题,并附带了完整的C++实现代码。

Count the string

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4899    Accepted Submission(s): 2313


题目连接


Problem Description
It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of this string. For example:
s: "abab"
The prefixes are: "a", "ab", "aba", "abab"
For each prefix, we can count the times it matches in s. So we can see that prefix "a" matches twice, "ab" matches twice too, "aba" matches once, and "abab" matches once. Now you are asked to calculate the sum of the match times for all the prefixes. For "abab", it is 2 + 2 + 1 + 1 = 6.
The answer may be very large, so output the answer mod 10007.
 

Input
The first line is a single integer T, indicating the number of test cases.
For each case, the first line is an integer n (1 <= n <= 200000), which is the length of string s. A line follows giving the string s. The characters in the strings are all lower-case letters.
 

Output
For each case, output only one number: the sum of the match times for all the prefixes of s mod 10007.
 

Sample Input
  
1 4 abab
 

Sample Output
  
6
 

Author
foreverlin@HNU
 

Source
HDOJ Monthly Contest – 2010.03.06

记忆化搜索: 预先记录前一状态的位置.

AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int M = 1e5 * 2 + 100;
char s[M];
int map[M];

int main()
{
    int T,n;
    scanf("%d",&T);
    while(T--)
    {
        memset(map,0,sizeof(map));
        scanf("%d %s",&n,s);
        int tp,len,ans;
        len = strlen(s);
        map[0] = ans = 0;
        tp = 1;
        for(int i = 1; s[i]; i++)
            if(s[i] == s[0]) map[tp++] = i;
        ans += tp;
        for(map[0]++; map[0] < len; map[0]++)
        {
            int temp = 1;
            for(int i = 1; i < tp; i++)
                if(s[map[i] + 1] == s[map[0]]) map[temp++] = map[i] + 1;
            tp = temp;
            ans += tp;
            if(ans > 10007) ans %= 10007;
        }
        printf("%d\n",ans);
    }
    return 0;
}


 
c++题目,代码无注释 T-2 Binary String and Score 分数 35 作者 曹鹏 单位 Amazon Given a binary string s (contains only characters 0 and 1), here is the way we calculate its score: Cut the string into several groups, each group contains the same character and consecutive groups contain different characters. The score is defined as the exclusive-or value of all the group lengths (if there is only one group, the score is its length). For instance, for string “10001100001”, the groups are “1”, “000”, “11”, “0000” and “1”. The score is 1 ^ 3 ^ 2 ^ 4 ^ 1 = 5. Now in each step, you can change the string by in swapping any adjacent 2 characters. You can apply as many as steps as you want (possible 0). For each possible score, how many different strings can you get and what’s the minimal number of steps to change s into a string with the particular score? Input Specification: Each input file contains one test case. Each case has a single line containing the binary string s (1 ≤ s.length() ≤ 64). Output Specification: For each possible score, output a line containing 3 space-separated integers, namely, the score, the number of different strings with the particular score you can get after changing s as many times as you want, and the minimal number of steps to change s into a string of that score. Output the lines in the score’s ascending order. Sample Input: 010 Sample Output: 1 1 0 3 2 1 Hint: There are 3 possible final strings in total. “010” needs has score 1 (1 ^ 1 ^ 1 = 1) and the minimal number of steps to get it is 0. Both “011” and “110” have score 3 (1 ^ 2) and the minimal number of steps to get them is 1. (swap the middle ‘1’ with the character on the left or right side.) 代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB 栈限制 8192 KB
08-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值