链接:https://ac.nowcoder.com/acm/problem/14547
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
双11又到了,小Z依然只是一只单身狗,对此他是如此的苦恼又无可奈何。
为了在这一天脱单小Z决定向女神表白,但性格腼腆的小Z决定隐晦一点,截取一段包含’L’、‘O’、‘V’、'E’的英文。(顺序不限)
小Z想起之前小D送给他一本英文书,决定在这里面截取一段话,小Z发现有好多种方案来截取这段话。
你能知道小Z能有多少种方案截取这段话么?
为了简化问题,英文文本讲不会出现空格、换行、标点符号,只有大写的情况。
输入描述:
本题有T组数据。
对于每组数据只有一行文本。
1≤T≤20
1≤文本长度≤100000
输出描述:
输出结果,并换行。
示例1
输入
3
ILOVEACM
LOVELOVE
ALBECVOD
输出
8
15
4
知识点:字符串
注意:按照顺序找,否则处理起重复计数问题来可能会比较乱。从某一下标 i i i 开始,找到包含四个字母的最短子串,末尾下标是 i d x idx idx,则从 i i i 开始截取的种数就是 n − i d x n-idx n−idx。
另外,find_first_of系列函数返回值是size_t,要转换一下,比如转成int,否则不能放到max min里面。
#include<bits/stdc++.h>
using namespace std;
int T;
string str;
typedef long long ll;
ll helper(){
int n=str.length();
int lastidx=n-1;
lastidx=min(lastidx, (int)str.find_last_of('L'));
lastidx=min(lastidx, (int)str.find_last_of('O'));
lastidx=min(lastidx, (int)str.find_last_of('V'));
lastidx=min(lastidx, (int)str.find_last_of('E'));
ll ans=0;
for(int i=0;i<=lastidx;i++){
int idx=i;
idx=max(idx, (int)str.find_first_of('L',i));
idx=max(idx, (int)str.find_first_of('O',i));
idx=max(idx, (int)str.find_first_of('V',i));
idx=max(idx, (int)str.find_first_of('E',i));
ans+=n-idx;
}
return ans;
}
int main(){
scanf("%d",&T);
for(int i=0;i<T;i++){
cin>>str;
cout<<helper()<<endl;
}
}