程序设计:后缀字符串

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

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

输入格式

第一行输入一个整数 nn。

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

输出格式

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

数据范围

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

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

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

样例输入

3
ba
a
aba

样例输出

2
3
1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
const int maxn = 1e5+10;
char s[maxn][11];
map<long long, int> hashMap;
long long anss[maxn];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
        int len=strlen(s[i]);
        long long ans=0;
        for(int j=len-1;j>=0;j--){
            ans=ans*26+s[i][j]-96;
            if(hashMap.find(ans)==hashMap.end())
                hashMap[ans]=1;
            else
                hashMap[ans]+=1;
        }
        anss[i]=ans;
    }
    for(int i=0;i<n;i++){
        printf("%d\n",hashMap[anss[i]]);
    }
    return 0;
}

 

posted on 2019-03-16 19:01  ggsdduzdl  阅读(131)  评论(0)    收藏  举报

导航