1099.前缀判断 SDNUOJ 1099
Description
给定 n 个字符串,求有多少字符串是其他字符串的前缀。
Input
第一行为一个整数n(1 <= n <= 1000),之后n行,每行一个字符串,字符串只由26个小写字母组成,最大长度为100。
Output
一个整数,有多少字符串是其他字符串的前缀。
Sample Input
5
abcde
ab
bcde
b
cde
Sample Output
2
#include <string>
#include <iostream>
using namespace std;
string s[1005];
int main()
{
int n;
while(cin >> n)
{
int cnt = 0;
for(int i = 0; i < n; ++i)
cin >> s[i];
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
{
if(j == i)
continue;
if(s[j].find(s[i], 0) == 0)
{
cnt++;
break;
}
}
cout << cnt << '\n';
}
return 0;
}