当前位置: 首页 > article >正文

P2444 [POI2000] 病毒

[P2444 POI2000] 病毒 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

问题等价于在字典图中找一个环,这个环要满足环上节点不是所有所给字符串最后一个字符结尾的点。

找环算法:

void dfs(int u) {
	in[u] = 1; // u在当前dfs路径中
	for(auto y: g[u]) {
		if(in[y]) {
			// 与dfs路径中点重合,成环
			fg = 1;
		}
		// 如果之前已经访问过y点,表示经过y点不会有环
		if(vis[y]) continue; 
		vis[y] = 1;
		dfs(y);
	}
    in[u] = 0;
}

本题找环,还需要保证,y点不是所给字符串中最后一个字符结尾的点。

代码:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <random>
#include <sstream>
#include <numeric>
#include <stdio.h>
#include <functional>
#include <bitset>
#include <algorithm>
using namespace std;

// #define Multiple_groups_of_examples
// #define int_to_long_long
#define IOS std::cout.tie(0);std::cin.tie(0)->sync_with_stdio(false); // 开IOS,需要保证只使用Cpp io流 *
#define dbgnb(a) std::cout << #a << " = " << a << '\n';
#define dbgtt cout<<" !!!test!!! "<<'\n';
#define rep(i,x,n) for(int i = x; i <= n; i++)

#define all(x) (x).begin(),(x).end()
#define pb push_back
#define vf first
#define vs second

typedef long long LL;
#ifdef int_to_long_long
#define int long long
#endif
typedef pair<int,int> PII;

const int INF = 0x3f3f3f3f;
const int N = 2e5 + 21;

namespace golitter {
namespace ACAM {
// https://www.luogu.com.cn/problem/P5357
// https://zhuanlan.zhihu.com/p/546999184
struct acam {
    static const int M = 300005;
    struct node {
        int fail;
        int ch[28] = {0};
    }tr[M];
    int cnt = 0;
    map<int,int> ref;
    vector<int> g[M];
    int ans[M];
    bitset<M> in, vis, isend;
    void insert(string &s, int id) {
        int u = 0;
        for(auto c: s) {
            int last = c == '_' ? 26 : c - 'a';
            if(!tr[u].ch[last]) {
                tr[u].ch[last] = ++cnt;
            }
            u = tr[u].ch[last];
        }
        isend[u] = 1;
        ref[id] = u;
    }
    void build() {
        queue<int> que;
        for(int i = 0; i < 27; ++i) {
            if(tr[0].ch[i]) que.push(tr[0].ch[i]);
        }
        while(que.size()) {
            int u = que.front(); que.pop();
            for(int i = 0; i < 27; ++i) {
                if(tr[u].ch[i]) {
                    tr[tr[u].ch[i]].fail = tr[tr[u].fail].ch[i];
                    que.push(tr[u].ch[i]);
                    if(isend[tr[tr[u].ch[i]].fail]) isend[tr[u].ch[i]] = 1;
                } else tr[u].ch[i] = tr[tr[u].fail].ch[i];
            }
        }
        for(int i = 1; i <= cnt; ++i) g[tr[i].fail].push_back(i);
    }
    void query(string &S) {
        int u = 0;
        for(auto c: S) {
            int last = c == '_' ? 26 : c - 'a';
            u = tr[u].ch[last];
            ans[u]++;
        }
    }
    bool fg = 0;
    bool check() {
        dfs(0);
        return fg;
    }
    void dfs(int u) {
        in[u] = 1;
        for(int i = 0; i <= 1; ++i) {
            if(in[tr[u].ch[i]]) {
                fg = 1;
                return ;
            }
            if(vis[tr[u].ch[i]] || isend[tr[u].ch[i]]) continue;
            vis[tr[u].ch[i]] = 1;
            dfs(tr[u].ch[i]);
        }
        in[u] = 0;
    }
    void clear() {
        for(int i = 0; i < M; ++i) {
            tr[i] = {};
            g[i].clear();
            ans[i] = 0;
        }
        ref.clear();
        cnt = 0;
    }
}trie;

}}
using golitter::ACAM::trie;
void inpfile();
void solve() {
    int n; cin>>n;
    for(int i = 0; i< n; ++i) {
        string s; cin>>s;
        for(auto&t: s) {
            if(t == '1') t = 'b';
            else t = 'a';
        }
        trie.insert(s, i);
    }
    trie.build();
    puts(trie.check() ? "TAK" : "NIE");
}
#ifdef int_to_long_long
signed main()
#else
int main()
#endif

{
    #ifdef Multiple_groups_of_examples
    int T; cin>>T;
    while(T--)
    #endif
    solve();
    return 0;
}
void inpfile() {
    #define mytest
    #ifdef mytest
    freopen("ANSWER.txt", "w",stdout);
    #endif
}

http://www.kler.cn/news/134686.html

相关文章:

  • 1688商品详情原数据(2023年11月最新版)
  • MySQL集群高可用架构之MMM
  • 八股文-TCP的四次挥手
  • “智能与未来”2024世亚国际智能机器人展会(简称:世亚智博会)
  • Swin Transformer
  • 云端援手:智能枢纽应对数字资产挑战 ——华为云11.11应用集成管理与创新专区优惠限时购
  • 图神经网络:消息传递算法
  • 使用JDK自带java.util.logging.Logger引起的冲突问题
  • HTTP(Hypertext Transfer Protocol)协议
  • Cadence virtuoso drc lvs pex 无法输入
  • AlmaLinux download
  • 开发中遇到的问题
  • HarmonyOS开发(四):应用程序入口UIAbility
  • 小米手环8pro重新和手机配对解决办法
  • java: 无法访问org.mybatis.spring.annotation.MapperScan
  • 智能货柜:无人零售行业的新宠
  • JDBC编程
  • 阿里云CentOS主机开启ipv6
  • Windows10下Maven3.9.5安装教程
  • 代码逻辑修复与其他爬虫ip库的应用
  • 探索SPI:深入理解原理、源码与应用场景
  • 【Matterport3D模拟器安装详细教程】适用于离散视觉语言导航任务的环境部署与安装
  • python django 小程序博客源码
  • 如何保护PayPal账户安全:防止多个PayPal账号关联?
  • 【服务器学习】hook模块
  • SIMULIA|Abaqus 2022x新功能介绍第三弹
  • 【面试经典150 | 数学】回文数
  • 计算机毕业设计选题推荐-个人博客微信小程序/安卓APP-项目实战
  • 硬盘无法格式化怎么办?
  • ModStartCMS v7.6.0 CMS备份恢复优化,主题开发文档更新