HDU 3336 Count the string / KMP
2014-03-25 11:00
148 查看
都是找和前缀一样的有多少个 想到KMP
然后要求的不是最长的前缀 只要是前缀都算 因此巧妙的借助Next数组 再来一个dp 有点前缀和的思想不好说 慢慢揣摩
然后要求的不是最长的前缀 只要是前缀都算 因此巧妙的借助Next数组 再来一个dp 有点前缀和的思想不好说 慢慢揣摩
#include <cstdio> #include <cstring> const int maxn = 200010; const int mod = 10007; char a[maxn]; int f[maxn]; int dp[maxn]; int n, m; void getFail() { f[0] = f[1] = 0; for(int i = 1; i < n; i++) { int j = f[i]; while(j && a[i] != a[j]) j = f[j]; f[i+1] = a[i] == a[j] ? j+1 : 0; } dp[0] = 0; for(int i = 1; i <= n; i++) { dp[i] = dp[f[i]]+1; m += dp[i]; m %= mod; } } int main() { int T; scanf("%d", &T); while(T--) { m = 0; scanf("%d %s", &n, a); getFail(); printf("%d\n", m); } return 0; }
相关文章推荐
- 【HDU 3336】Count the string(kmp)
- hdu 3336 Count the string (kmp扩展)
- Hdu 3336 Count the String(DP+KMP)(好题)
- hdu 3336 Count the string 【kmp + dp】
- HDU_3336 Count the string(KMP)
- hdu 3336 Count the string(KMP)
- HDU 3336 Count the string (kmp+dp)
- hdu 3336 Count the string(KMP)
- hdu 3336 Count the string(KMP+dp)
- hdu 3336 Count the string kmp
- HDU 3336 Count the string (kmp+dp)
- HDU 3336 Count the string 【KMP】【dp】
- HDU 3336 Count the string(kmp + dp)
- HDU 3336 Count the string KMP+DP
- HDU 3336 Count the string (KMP next数组运用——统计前缀出现次数)
- hdu 3336 Count the string -KMP&dp
- hdu -- 3336 Count the string(KMP + dp)
- HDU 3336 Count the string KMP+DP
- Count the string (HDU_3336) 扩展KMP + dp
- hdu 3336 Count the string(思维可水过,KMP)