HDOJ 5672//類比

來源:互聯網
上載者:User

標籤:

題目:http://acm.hdu.edu.cn/showproblem.php?pid=5672

題意:有一個字串S,字串裡面只包含小寫字母,問有多少個子串裡面有至少K個不同的字母;

思路:還是放在代碼裡面說會好一點,其實就是維護一個左端點和滿足性質的最小右端點的過程。

 1 #include <stdio.h> 2 #include <iostream> 3 #include <stdlib.h> 4 #include <algorithm> 5 #include <string.h> 6 #define N 1000100 7 char s[N]; 8 int vis[N]; 9 int t;10 int k;11 int main()12 {13     scanf("%d",&t);14     while(t--)15     {16         scanf("%s",s);17         scanf("%d",&k);18         int len=strlen(s);19         memset(vis,0,sizeof(vis));//標記字母出現的次數20         long long int cnt=0,ans=0,len1=-1,sum=0;21         for(int i=0;i<len;i++)22         {23             while(cnt!=k&&len1<len)24             {25                 len1++;26                 if(len1>=len)27                     break;28                     if(vis[s[len1]-‘a‘]==0)29                         cnt++;//每有一個不同的那麼計數器就加一30                     vis[s[len1]-‘a‘]++;31 32             }33             if(len1>=len)//枚舉完了就跳出34                 break;35                 ans=ans+(len-len1);//如果(i,j)滿足這個性質,那麼(i,k)(K>=J)都滿足這個性質36             if(--vis[s[i]-‘a‘]==0)//如果這個滿足這個性質的子串裡面這個字母只出現了一次,那麼這個字母之後肯定是滿足不了這個性質的,也就是不同的字母會少一個,就要重新再找一次滿足性質的最小的右端點37                     cnt--;38         }39         printf("%lld\n",ans);40     }41     return 0;42 }
View Code

 

HDOJ 5672//類比

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.