本来是个后缀数组,考察算法的中级题目,暴力居然也可以水过,就看你跳不跳坑了(c++和G++返回结果就很不一样,关键看编译器)
丝毫不差的代码,就看运气如何了。唯一差别c++还是G++,但正解是后缀数组没错,趁机学一下吧。
1 #include <cstdio>
2 #include <iostream>
3 #include <sstream>
4 #include <cmath>
5 #include <cstring>
6 #include <cstdlib>
7 #include <string>
8 #include <vector>
9 #include <map>
10 #include <set>
11 #include <queue>
12 #include <stack>
13 #include <algorithm>
14 using namespace std;
15 #define ll long long
16 #define _cle(m, a) memset(m, a, sizeof(m))
17 #define repu(i, a, b) for(ll i = a; i < b; i++)
18 #define repd(i, a, b) for(ll i = b; i >= a; i--)
19 #define sfi(n) scanf("%I64d", &n)
20 #define MAXN 100010
21 #define N 100010
22 ll n,num1;
23 char s[MAXN];
24 ll a[N],b[N];
25 ll solve()
26 {
27 ll num = num1;
28 repu(i,1,n)
29 {
30 ll st1 = a[i],st2 = a[i-1];
31 int t = 0;
32 while(s[st1] == s[st2]&& st1 < b[i] && st2 < b[i-1])
33 {
34 t++;
35 st1++;
36 st2++;
37 }
38 if(t == 0)
39 num += 1;
40 else
41 num += (int)log10(t) + 1;
42 num += 1;
43 num -= t;
44 }
45 return num + 2;
46 }
47 int main()
48 {
49 while(~scanf("%s",s))
50 {
51 scanf("%I64d",&n);
52 num1 = 0;
53 repu(i,0,n)
54 {
55 scanf("%I64d%I64d",&a[i],&b[i]);
56 num1 += (b[i] - a[i]);
57 }
58 num1 += n;
59 ll num2 = solve();
60 printf("%I64d %I64d\n",num1,num2);
61 }
62 return 0;
63 }
后缀数组待学习: