[题目大意]
有两个长度为m,n的字符串,以及查询次数q次;每次查询提供两个数据a&&b,问在长度为m的字符串的第a到第b个字符的区域内有多少个n这样的字符串。
[总结]
看似很水的一道题居然没做出来。发现自己对于数据的输入输出还是存在问题。看了eddy的题解,真心惊叹。除了两分多钟的AC时间还有对于string字符串的理解以及其基础函数(例如substr)的运用,以及最后对数组的处理,代码整体真的很惊艳。
[eddy题解]
(直接上代码)代码倒数第三行略有错误,目前cf貌似崩了,有机会更正。
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=1021;
int n, m, q, ans[N];
string s, t;
int main()
{
scanf("%d%d%d", &n, &m, &q);
cin>>s>>t;
for(int i=0; i+m<=n; i++)
if(s.substr(i, m) == t)
ans[i+1]++;
for(int i=2; i<=n; i++)
ans[i]+=ans[i-1];
while(q--)
{
int li, ri;
scanf("%d%d", &li, &ri);
ri=ri-m+1;
printf("%d
", max(0, ans[max(0,ri)]-ans[li-1]));//tiny mistake
}
}
[题解优点]
substr———待补
// string::substr
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
std::cout << str2 << ' ' << str3 << '
';
return 0;
}
来自cpp官网解释。