Where Am I?【字符串比较】

题目描述:原题链接

Where Am I?【字符串比较】_第1张图片

思路:

因为N只有100,所以从头往后一次比较,利用到了strncmp函数,第三个参数代表比较前k个的字符。


#include 
#include 
#include 
#include 
using namespace std;

char s[200];
int main()
{
    int n;
    cin>>n>>s;
    for(int k = 1; k <= n; k++)
    {
        bool flag = true;
        for (int i = 0; flag && (i + k <= n); i++)
        {
            for (int j = i + 1; flag && (j + k <= n); j++)
            {
                if (strncmp(s + i, s + j, k) == 0)
                    flag = false;
            }
        }
        if (flag)
        {
            cout<<k<<endl;
            break;
        }
    }
}

你可能感兴趣的:(Where Am I?【字符串比较】)