一天蒜头君得到 n个字符串 si,每个字符串的长度都不超过 10。
蒜头君在想,在这 n 个字符串中,以 si 为后缀的字符串有多少个呢?
输入格式
第一行输入一个整数 n。
接下来 n 行,每行输入一个字符串 si。
输出格式
输出 n 个整数,第 i个整数表示以 si 为后缀的字符串的个数。
数据范围
对于 50% 的数据,1<=n <=10^3。
对于 100% 的数据,1<=n <=10^5。
所有的字符串仅由小写字母组成。
样例输入
3
ba
a
aba
样例输出
2
3
1
学了一下hash,虽然这题好像用hash挺多余(……)不管了就当学习hash了((
题意就是统计以 si 为后缀的字符串的个数
#include <iostream>
#include <string>
#include <map>
#include <cstring>
#include <algorithm>
#define ull unsigned long long
using namespace std;
ull base=131;
int prime=233317;
ull mod=212370440130137957ll;
ull h[100010]={0};
ull Hash(string str){//将字符串转换成对应整数
int len=str.length(),i;
ull ans=0;
for(i=0;i<len;i++){
ans=(ans*base+(ull)str[i])%mod+prime;
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
map <ull,int> mp;
int n,i,j;
cin>>n;
string s,t;
for(i=0;i<n;i++){
cin>>s;
reverse(s.begin(),s.end());
for(j=1;j<=s.length();j++){
t=s.substr(0,j);
mp[Hash(t)]++;
}
h[i]=Hash(s);
}
for(i=0;i<n;i++){
cout<<mp[h[i]]<<endl;
}
return 0;
}