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;
}

全部评论

相关推荐

01-18 09:26
已编辑
门头沟学院 Java
王桑的大offer:建议中间件那块写熟悉即可,写掌握 面试包被拷打到昏厥
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务