CodeForces 1384A. Common Prefixes
阿新 • • 發佈:2020-07-26
題意:字串s和t的最長公共字首(lcp)被定義為最大的整數k(0 <= k <= min(n, m)),使得s1s2...sk等於t1t2...tk。一個人一開始有n + 1個字串s1s2...sn+1,對於i(1 <= i <= n),這個人計算了ai---si和si+1的最長公共字首。現在給定a1a2...an,求構造n+1個字串?
分析:ai的最大長度為50,我們可以先構造一個長度大於50的字串,全是'a'的字母,每次構造一個字串的時候,先拷貝上一個字串,然後修改最長公共字首的下一個字母,使得這個字串和上一個字串不同即可。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int N = 105; int a[N]; void modify(int pos, string& s) { char c = s[pos]; if (c + 1 > 'z') c = 'a'; else ++c; s[pos] = c; } int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); string s; for (int i = 1; i <= 55; ++i) { s += 'a'; } cout << s << endl; for (int i = 1; i <= n; ++i) { modify(a[i], s); cout << s << endl; } } return 0; }