CF1931F Chat Screenshots 另一种题解
题目链接:CF 或者 洛谷
本题拓扑排序不再赘述,来说说字符串哈希怎么做这题。
本篇以另一种角度剖析题目背景,并不追求最优,例如有些地方其实可以暴力判断,主要以构造的角度阐述,顺便感谢灵茶山的朋友的讨论。
结论
三个串及其以上必定能构造出最初的那个串。
下面进行证明:
首先一个串,显然有多种可能,我们从两个串的构造开始阐述。
对于两个串而言,我们可以轻松构造出反例,如果开头的两个字符在初始串中处于相邻位置,那么很显然无论把谁提到开头,都无法确定出偏序关系,并不知道这两个字符谁前谁后。但很显然这两个字符并不会影响其他的偏序关系,我们可以在两个串中分别去掉这两个字符,然后直接比对剩余串。例如:
1 2 3 4
2 3 1 4,去掉
三个串的构造
这里说说一种基于偏序关系的构造方式,当然肯定还有其他的方式。
首先一个显而易见的结论,对于任何一个模式串来说,第一个字符或者第二个字符一定是初始串的开头位置。
证明如下:
如果我们所提到开头的字符即为初始串开头字符,那么结论成立。如果非开头字符,则原先的开头字符顺位成为第二个字符。而我们在任意一个串中去寻找初始串只需要看两件事:
-
找到前两个开头字符哪个是初始开头。
-
将另一个非初始开头移至正确位置。
第一个问题,我们只需要确定前两个字符的偏序关系即可。这个至多需要两个串。如果第二个字符,恰好为第二个串的开头,即例如:第一个串开头为
这里要重新强调一点:提取字符到开头仅仅影响开头字符与其他字符的偏序,而不会影响其他字符之间的偏序,后文中会反复使用。
显然我们只需要找到开头字符不为第二个字符的串即可,抛开第一个待判断串以外,我们至多需要两个串即可找到二者偏序关系。
第一点找到以后,如果恰好开头字符即为初始字符串的开头字符,显然我们是将 开头提到了开头,第一个串本身其实就是初始串。
否则第二个字符是开头字符,我们只需要找到第一个字符的原来位置即可。
下面阐述我个人的寻找方式,首先找到在两个串当中这个需要待插入的字符的
-
相同说明,这个
就是正确的了,我们在插入时访问到 就插入这个字符即可。 -
不同说明,原位置前有某个字符被移动到了开头。这两个串当中不同的
一定有一个是真正的 ,那个不是真正的 的仅仅是因为被移到了开头,我们只需要由第一个串反过来确定第二个和第三个串的 的偏序关系,后面的那个显然即为真正的 。
证明如下:
第一种情况很好证明,说明
第二种情况也很好证明,我们记真正的
解法
当拿到初始串以后,我们可以预处理出前缀字符串哈希以及每个数的位置。我们遍历每个待判断串,我们去掉开头剩余的子串的哈希应该是等价于初始串去掉该字符拼出来的子串哈希相等。
例如初始串是
参照代码
#include <bits/stdc++.h> // #pragma GCC optimize(2) // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") // #define isPbdsFile #ifdef isPbdsFile #include <bits/extc++.h> #else #include <ext/pb_ds/priority_queue.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/tree_policy.hpp> #include <ext/pb_ds/trie_policy.hpp> #include <ext/pb_ds/tag_and_trait.hpp> #include <ext/pb_ds/hash_policy.hpp> #include <ext/pb_ds/list_update_policy.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/exception.hpp> #include <ext/rope> #endif using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tii; typedef tuple<ll, ll, ll> tll; typedef unsigned int ui; typedef unsigned long long ull; typedef __int128 i128; #define hash1 unordered_map #define hash2 gp_hash_table #define hash3 cc_hash_table #define stdHeap std::priority_queue #define pbdsHeap __gnu_pbds::priority_queue #define sortArr(a, n) sort(a+1,a+n+1) #define all(v) v.begin(),v.end() #define yes cout<<"YES" #define no cout<<"NO" #define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr); #define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout); #define forn(i, a, b) for(int i = a; i <= b; i++) #define forv(i, a, b) for(int i=a;i>=b;i--) #define ls(x) (x<<1) #define rs(x) (x<<1|1) #define endl '\n' //用于Miller-Rabin [[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; template <typename T> int disc(T* a, int n) { return unique(a + 1, a + n + 1) - (a + 1); } template <typename T> T lowBit(T x) { return x & -x; } template <typename T> T Rand(T l, T r) { static mt19937 Rand(time(nullptr)); uniform_int_distribution<T> dis(l, r); return dis(Rand); } template <typename T1, typename T2> T1 modt(T1 a, T2 b) { return (a % b + b) % b; } template <typename T1, typename T2, typename T3> T1 qPow(T1 a, T2 b, T3 c) { a %= c; T1 ans = 1; for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c; return modt(ans, c); } template <typename T> void read(T& x) { x = 0; T sign = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-')sign = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } x *= sign; } template <typename T, typename... U> void read(T& x, U&... y) { read(x); read(y...); } template <typename T> void write(T x) { if (typeid(x) == typeid(char))return; if (x < 0)x = -x, putchar('-'); if (x > 9)write(x / 10); putchar(x % 10 ^ 48); } template <typename C, typename T, typename... U> void write(C c, T x, U... y) { write(x), putchar(c); write(c, y...); } template <typename T11, typename T22, typename T33> struct T3 { T11 one; T22 tow; T33 three; bool operator<(const T3 other) const { if (one == other.one) { if (tow == other.tow)return three < other.three; return tow < other.tow; } return one < other.one; } T3() { one = tow = three = 0; } T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three) { } }; template <typename T1, typename T2> void uMax(T1& x, T2 y) { if (x < y)x = y; } template <typename T1, typename T2> void uMin(T1& x, T2 y) { if (x > y)x = y; } constexpr ll N = 2e5 + 10; ll pw[N]; ll m; int n; constexpr ll MOD = 1e9 + 7; constexpr ll BASE = 33; ll Inv = qPow(BASE, MOD - 2, MOD); int start[N]; inline void solve() { cin >> n >> m; vector a(m, vector<int>(n)); for (auto& x : a)for (auto& y : x)cin >> y; if (m == 1) { yes << endl; return; } const int pre1 = a[0][0], pre2 = a[1][0], pre3 = a[0][1]; if (m == 2) { //也可以直接双指针判断 ll x = 0, y = 0; int idx1 = 0, idx2 = 0; forn(i, 0, n-1) { if (a[0][i] != pre1 and a[0][i] != pre2)x = (x + a[0][i] * pw[idx1++] % MOD) % MOD; if (a[1][i] != pre1 and a[1][i] != pre2)y = (y + a[1][i] * pw[idx2++] % MOD) % MOD; } cout << (x == y ? "YES" : "NO") << endl; return; } //判断哪个串是开头不为第二个字符 const int t = a[1][0] == pre3 ? 2 : 1; int isPre = 0; forn(i, 1, n-1) { //找偏序关系靠前的那个为开头字符 if (a[t][i] == pre1 or a[t][i] == pre3) { isPre = a[t][i]; break; } } int idx = 0; if (isPre == pre1) { //本身就是初始串 forn(i, 0, n-1)start[++idx] = a[0][i]; } else { //找待插入位置的pre int preVal = 0; forn(i, 1, n-1) { if (a[1][i] == pre1) { preVal = a[1][i - 1]; break; } } forn(i, 1, n-1) { if (a[2][i] == pre1) { if (a[2][i - 1] != preVal) { //相等就是本身 //二者的pre如果不等,通过第一个串的偏序关系确认,靠后的才是真正的pre forv(ans, n-1, 1) { if (a[0][ans] == preVal or a[0][ans] == a[2][i - 1]) { preVal = a[0][ans]; break; } } } break; } } forn(i, 0, n-1) { if (a[0][i] == pre1)continue; start[++idx] = a[0][i]; if (a[0][i] == preVal)start[++idx] = pre1; //找到pre就顺带插入 } } vector<ll> pre(n + 1), pos(n + 1); forn(i, 1, n)pre[i] = (pre[i - 1] + start[i] * pw[i] % MOD) % MOD, pos[start[i]] = i; forn(i, 0, m-1) { ll t1 = 0; forn(j, 1, n-1)t1 = (t1 + a[i][j] * pw[j] % MOD) % MOD; //直接考虑除开开头串的剩余串哈希 const int id = pos[a[i][0]]; //分割点位置 ll t2 = (pre[id - 1] + modt(pre[n] - pre[id], MOD) * Inv % MOD) % MOD; //拼串记得有除法需要逆元 if (t1 != t2) { no << endl; return; } } yes << endl; } signed int main() { // MyFile Spider //------------------------------------------------------ // clock_t start = clock(); int test = 1; // read(test); cin >> test; pw[0] = 1; forn(i, 1, N-1)pw[i] = pw[i - 1] * BASE % MOD; forn(i, 1, test)solve(); // while (cin >> n, n)solve(); // while (cin >> test)solve(); // clock_t end = clock(); // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统