hdu 336

Count the string

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

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define mmax 200000+10
int next[mmax],ans[mmax],n;
char p[mmax];
void get_next(){
    memset(next,0,sizeof(next));
    for(int i=1;i<n;i++){
        int j=i;
        while(j>0){
            j=next[j];
            if(p[i]==p[j]){
                next[i+1]=j+1;
                break;
            }
        }
    }
}
void solve(){
    int sum=0;
    memset(ans,0,sizeof(ans));
    for(int i=1;i<=n;i++){
        ans[i]=(ans[next[i]]+1)%10007;
        sum+=ans[i];
    }
    cout<<sum%10007<<endl;
}
int main(){
    int t;cin>>t;
    while(t--){
        scanf("%d",&n);
        scanf("%s",p);
        get_next();
        solve();
    }
}
时间: 2024-11-09 04:57:49

hdu 336的相关文章

【DFS求树的最大二分匹配+输入外挂】HDU 6178 Monkeys

http://acm.hdu.edu.cn/showproblem.php?pid=6178 [题意] 给定一棵有n个结点的树,现在有k个猴子分布在k个结点上,我们可以删去树上的一些边,使得k个猴子每个猴子都至少和其他一个猴子相连 问树上最少保留多少条边 [思路] 每个猴子要至少和一个猴子相连,考虑保留的边最少,那么最优的情况一定是一条边的两个顶点放两个猴子,这些边的顶点都不重合 我们现在要找到给定的树中最多有多少条这样的边,即最大二分匹配 O(n)的DFS,对于每个结点,优先与叶子结点形成一条

HDU 1058 Humble Numbers(离线打表)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1058 解题报告:输入一个n,输出第n个质因子只有2,3,5,7的数. 用了离线打表,因为n最大只有5842. 1 #include<stdio.h> 2 #define INT __int64 3 INT ans[5850] = { 4 0,1,2,3,4,5,6,7,8,9,10,12,14,15,16,18,20,21,24,25,27,28,30,32,35,36,40,42,45,48,4

HDU 1350 最小路径覆盖

Taxi Cab Scheme Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 707    Accepted Submission(s): 336 Problem Description Running a taxi station is not all that simple. Apart from the obvious dem

hdu 4864 Task(贪婪啊)

主题链接:pid=4864">http://acm.hdu.edu.cn/showproblem.php?pid=4864 Task Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1346    Accepted Submission(s): 336 Problem Description Today the company h

HDU 1691 Chinese Chess

严格按规则实现.使用多态加异常来简化代码逻辑 Player类表示玩家 存储身份信息和持有棋子信息 Board类表示棋盘 可以通过坐标得到棋子 Piece类表示棋子 存储位置信息和持有人信息 各个棋子的实现提供对棋子走法的支持 Game类控制程序逻辑 具体见代码 1 package acm.hdu.p1691; 2 3 import java.io.FileInputStream; 4 import java.util.ArrayList; 5 import java.util.List; 6 i

hdu 4531 bfs(略难)

题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 1 /* 2 *HDU 4531 3 *BFS 4 *注意判重 5 */ 6 7 8 #include <stdio.h> 9 #include <string.h> 10 #include <algorithm> 11 #include <map> 12 #include <set> 13 #include <string> 14 #include <queue&g

[2019HDU多校第一场][HDU 6590][M. Code]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6590 题目大意(来自队友):二维平面上有\(n\)个点,每个点要么是黑色要么是白色,问能否找到一条直线将平面分割成黑白两部分 题解:分别对每种颜色的点求凸包,判断是否相交即可. (有模板真好) 1 #include<bits/stdc++.h> 2 //#include<cstdio> 3 //#include<cmath> 4 //#include<algorith

HDU 6203 ping ping ping [LCA,贪心,DFS序,BIT(树状数组)]

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6203] 题意 :给出一棵树,如果(a,b)路径上有坏点,那么(a,b)之间不联通,给出一些不联通的点对,然后判断最少有多少个坏点. 题解 :求每个点对的LCA,然后根据LCA的深度排序.从LCA最深的点对开始,如果a或者b点已经有点被标记了,那么continue,否者标记(a,b)LCA的子树每个顶点加1. #include<Bits/stdc++.h> using namespace std;

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include