蓝桥杯省赛模拟题-后缀字符串

一天蒜头君得到 nn 个字符串 s_isi​,每个字符串的长度都不超过 1010。

蒜头君在想,在这 nn 个字符串中,以 s_isi​ 为后缀的字符串有多少个呢?

输入格式

第一行输入一个整数 nn。

接下来 nn 行,每行输入一个字符串 s_isi​。

输出格式

输出 nn 个整数,第 ii 个整数表示以 s_isi​ 为后缀的字符串的个数。

数据范围

对于 50\%50% 的数据,1 \le n \le 10^31≤n≤103。

对于 100\%100% 的数据,1 \le n \le 10^51≤n≤105。

所有的字符串仅由小写字母组成。

样例输入复制

3
ba
a
aba

样例输出复制

2
3
1

题目来源

2019 蓝桥杯省赛 B 组模拟赛(一)

 

因为题目中只包含a-z小写字母,所以可以把字符串看为26进制的数字,然后用map记录下在二十六进制转换为十进制时每个后缀出现的次数

#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
const int MAXN(5*1e5);
const LL INF(0x7f7f7f7f7f7f7f7f);
map<LL,int>mp;
LL a[MAXN+50];
int main() {
    int n;
    char str[20];
    scanf("%d",&n);
    for(int i=1;i<=n;i++) {
        scanf("%s",str);
        LL temp=0,digit=1;
        for(int j=strlen(str)-1;j>=0;j--) {
            temp=temp+(str[j]-'a'+1)*digit;
            mp[temp]++;
            digit*=26;
        }
        a[i]=temp;
    }
    for(int i=1;i<=n;i++) {
        cout<<mp[a[i]]<<endl;
    }
}


 

    原文作者:停车场模拟问题
    原文地址: https://blog.csdn.net/Akatsuki__Itachi/article/details/88682003
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞