设为首页 加入收藏

TOP

BestCoder Round #11 (Div. 2)(二)
2015-07-20 17:35:44 来源: 作者: 【 】 浏览:8
Tags:BestCoder Round #11 Div.
mission(s): 194 Accepted Submission(s): 78


Problem Description You are given a string S consisting of lowercase letters, and your task is counting the number of substring that the number of each lowercase letter in the substring is no more than K.
Input In the first line there is an integer T , indicates the number of test cases.
For each case, the first line contains a string which only consist of lowercase letters. The second line contains an integer K.

[Technical Specification]
1<=T<= 100
1 <= the length of S <= 100000
1 <= K <= 100000
Output For each case, output a line contains the answer.
Sample Input
3
abc
1
abcabc
1
abcabc
2

Sample Output
6
15
21

Source BestCoder Round #11 (Div. 2)
意解:贪心,运用字符串的子串的前缀和技巧,和维护当前字母如果当前的字母不符合条件,则一一减去;

AC代码:
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       using namespace std; typedef long long ll; const int M = 1e5 + 100; vector
      
       dp[30]; char s[M]; int k; void solve() { int p = -1; ll ans = 0; for(int i = 0; i < 26; i++) dp[i].clear(); scanf("%s %d",s,&k); for(int i = 0; s[i]; i++) { int u = (int)(s[i] - 'a'); dp[u].push_back(i); if(dp[u].size() > k) { p = max(p,dp[u][dp[u].size() - k - 1]); } ans += (i - p); } printf("%I64d\n",ans); } int main() { int T; scanf("%d",&T); while(T--) { solve(); } return 0; } 
      
     
    
   
  




首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Codeforces 145A-Lucky Conversion 下一篇nyoj 1078 汉诺塔(四)[二分图 |..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·在 Redis 中如何查看 (2025-12-26 03:19:03)
·Redis在实际应用中, (2025-12-26 03:19:01)
·Redis配置中`require (2025-12-26 03:18:58)
·Asus Armoury Crate (2025-12-26 02:52:33)
·WindowsFX (LinuxFX) (2025-12-26 02:52:30)