首页 > 技术文章 > Subsequences in Substrings Kattis - subsequencesinsubstrings (暴力)

letlifestop 2019-04-08 20:35 原文

题目链接:

Subsequences in Substrings

 Kattis - subsequencesinsubstrings 

题目大意:给你字符串s和t。然后让你在s的所有连续子串中,找出这些连续子串中的非连续子串中包含t的有多少个。

具体思路:我们枚举s的每一个位置,然后判断一下s的包含t的非连续子串中到达那个位置,然后这个字符串两边的长度相乘就是当前位置开始,满足条件的字符位置。然后我们在找从上一次找到首字母位置下一个开始, 继续往下找就可以了。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 # define ll long long
 4 const int maxn = 4e5+100;
 5 int main()
 6 {
 7     ios::sync_with_stdio(false);
 8     string s,t;
 9     cin>>s>>t;
10     ll len1=s.size();
11     ll len2=t.size();
12     ll ans=0;
13     ll pos=-1;
14     while(1)
15     {
16         int st,ed;
17         st=s.find(t[0],pos+1);
18         if(st==-1)break;
19         ed=st;
20         for(int i=1;i<len2;i++){
21         ed=s.find(t[i],ed+1);
22         if(ed==-1)break;
23         }
24         if(ed==-1)break;
25         ans+=(st-pos)*(len1-ed);
26         pos=st;
27     }
28     printf("%lld\n",ans);
29 }

 

推荐阅读