Delete At Most Two Characters

Given a string which contains only lower case English letters, how many different strings you can get after deleting AT MOST TWO characters in it?

Input Specification:

Each input file contains one test case, which gives the string whose length is in [.

Output Specification:

Print in a line the number of different strings you can get after deleting at most 2 characters.

Sample Input:

ababcc
 

Sample Output:

15
 

Hint:

Deleting 0 character gets ababcc.

Deleting 1 character gets babccaabccabbccabacc and ababc.

Deleting 2 character gets abccbbccbaccbabcaaccaabcabbcabac and abab.

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     ios::sync_with_stdio(false);
 6  //     freopen("data.txt","r",stdin);
 7       string ss;
 8       cin>>ss;
 9       long long sum,k=ss.size();
10       long long t=1+k+k*(k-1)/2,con=1,scon=0,spart=0;
11       for(long long i=1;i<k;i++)
12       {
13           if(ss[i]==ss[i-1])
14         {
15               con++;
16               t--;
17         }
18         else
19         {      
20               if(con>1)
21               {
22                 if(spart)
23                 t=t-scon*con+spart;
24                 
25                 t=t-con*(con-1)/2+1;
26                 scon=scon+con;
27                 spart++;
28                }
29               con=1;
30               if(i>1&&ss[i]==ss[i-2])
31               t--;
32         }
33       }
34       if(con>1)
35       {
36         if(spart)
37         t=t-scon*con+spart;
38         t=t-con*(con-1)/2+1;
39            scon=scon+con;
40            spart++;
41       }
42       if(spart)
43       t=t-(k-scon)*(scon-spart);
44       
45       cout<<t;
46     return 0;
47 }

 

posted @ 2020-02-09 00:29  一斜星辰酱  阅读(220)  评论(0编辑  收藏  举报