牛客13253
思路:
简单模拟 + kmp
此外,本题也可以用s.find()函数【if (s.find(t) != -1)】
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 1e6;
int ne[N];
// s是长文本,p 是模板串,n是s的长度,m是p的长度
bool kmp (string s, string p)
{
int n = s.length (), m = p.length ();
memset (ne, 0, sizeof ne);
for (int i = 2, j = 0; i < m; i ++)
{
while (j && p[i] != p[j + 1])
j = ne[j];
if (p[i] == p[j + 1])
j ++;
ne[i] = j;
}
for (int i = 1, j = 0; i < n; i ++)
{
while (j && s[i] != p[j + 1])
j = ne[j];
if (s[i] == p[j + 1])
j++;
if (j == m - 1)
{
j = ne[j];
return true;
}
}
return false;
}
// 把int转化为char
char get (int x)
{
if (x <= 9) return x + '0';
return x - 10 + 'A';
}
// 将n转化为k进制
string turn_base (int n, int k)
{
string num = "";
while (n)
num += get (n % k), n /= k;
reverse (num.begin (), num.end ());
return num;
}
// s(n, k)
string base (int n, int k)
{
int nn = 1;
string basek = "";
while (nn <= n)
{
string tmp = turn_base (nn, k);
basek += tmp;
nn ++;
}
return basek;
}
int main ()
{
int n;
string t;
cin >> n >> t;
int k = 2;
if (n == 1 && t == "1")
{
cout << "yes" << endl;
return 0;
}
// cout << base (5, 2) << endl << base (16, 16) << endl;
while (k <= 16)
{
string s = base (n, k);
// cout << base (5, 2) << endl;
// cout << p << endl;
// cout << t << endl;
if (kmp (s, t))
{
cout << "yes" << endl;
return 0;
}
else
{
k ++;
continue;
}
// cout << base (n, k) << endl;
}
cout << "no" << endl;
}