HUST1718(ac自动机)

该博客讲述了如何解决一个字符串问题,即给定一个主键(master key)和多个仆从键(servant keys),找出每个仆从键在主键中最长的公共前缀长度。通过构建AC自动机并利用其进行匹配,可以找到每个仆从键的最长匹配前缀。样例输入和输出展示了具体操作过程。

1718 - Key

Time Limit: 2s Memory Limit: 256MB
Submissions: 114 Solved: 9
DESCRIPTION
A key is a string consisting of ‘E’, ‘S’, ‘W’ and ‘N’s. Some of keys are called master keys, and the others are called servant keys. Give you one master key KM and N servant keys (S1 to SN). You are asked to determine what the length of the longest prefix which can be found in the master key is for every servant key.
INPUT
The first line of the input gives the number of test cases T. T test cases follow.
Each test case starts with a line consist of one string representing KM. The next line consists of one integer N. Then N lines follows. The ith line consists of one string representing Si.
1 ≤ |KM| ≤ 106.
1 ≤ N ≤ 104.
1 ≤ |Si| ≤ 102.
KM and Si consists of only ‘E’, ‘S’, ‘W’, and ‘N’.
1 ≤ the total length of all keys in one input file ≤ 4 × 106.
OUTPUT
For each test case, first output one line containing “Case #x:”, where x is the test case number.
Then for every servant key, output one line consists of one integer indicating the length of the longest prefix which can be found in the master key.
SAMPLE INPUT
2
SNNSSNS
3
NNSS
NNN
WSEE
NSWWEEW
3
NNSW
EEW
WWEE
SAMPLE OUTPUT
Case #1:
4
2
0
Case #2:
1
3
4
HINT
SOURCE
lizhen

题意:给你一个字符串KM,然后给你n个字符串,问你这n个字符串中的每一个串能在KM串中找到一个最长的匹配前缀是多长。

解题思路:对于这个n个字符串建立ac自动机,然后与KM串进行匹配,匹配过程中,给能匹配上的节点打上标记就行,然后每个串能够匹配上的最长前缀就是,从根节点到该节点路径上的最远的那个打上标记的长度。

#include<bits/stdc++.h>
using namespace std;
int N;
char KM[1000005];
char S[10005][105];
struct node{
    int d;
    int h;
    node* Next[4];
    node* fail;
    bool flag;//
    bool visit;
};
node LiZhiMing[1000005];
node* re[10005];
int cnt;
node* root;
inline node* getV()
{
    LiZhiMing[cnt].d = 0;
    LiZhiMing[cnt].fail = NULL;
    LiZhiMing[cnt].flag = false;
    LiZhiMing[cnt].h = 0;
    LiZhiMing[cnt].visit = false;
    memset(LiZhiMing[cnt].Next, 0, sizeof(LiZhiMing[cnt].Next));
    return &LiZhiMing[cnt++];
}
void Insert(char *s,int id)
{
    node* p = root;
    for(int i = 0; s[i]; i++)
    {
        int num;
        if(s[i] == 'E') num = 0;
        else if(s[i] == 'S') num = 1;
        else if(s[i] == 'W') num = 2;
        else num = 3;
        if(p->Next[num] == NULL)
        {
            node* res = getV();
            p->Next[num] = res;
        }
        p->Next[num]->d = i + 1;
        p = p->Next[num];
    }
    re[id] = p;
}
void bfs()
{
    queue<node*> Q;
    while(!Q.empty()) Q.pop();
    Q.push(root);
    node* nx;
    while(!Q.empty())
    {
        nx = Q.front();
        Q.pop();
        int h = nx->h;
        for(int i = 0; i < 4; i++)
        {
            if(nx->Next[i])
            {
                if(nx->Next[i]->flag)
                {
                    nx->Next[i]->h = nx->Next[i]->d;
                }
                else
                {
                    nx->Next[i]->h = h;
                }
                Q.push(nx->Next[i]);
            }
        }

    }

}
//void Search(char *s, int id)
//{
//    node* p = root;
//    for(int i = 0; s[i]; i++)
//    {
//        int num;
//        if(s[i] == 'E') num = 0;
//        else if(s[i] == 'S') num = 1;
//        else if(s[i] == 'W') num = 2;
//        else num = 3;
//        if(p->Next[num]->flag)
//        {
//            dp[id] = max(dp[id], p->Next[num]->d);
//        }
//        else break;
//        p = p->Next[num];
//    }
//}
void build_fail()
{
    node* p = root;
    node* ans;
    node* nx;
    queue<node*> Q;
    Q.push(p);
    while(!Q.empty())
    {
        nx = Q.front(); Q.pop();
        for(int i = 0; i < 4; i++)
        {
            if(nx->Next[i])
            {
                if(nx == root)
                {
                    nx->Next[i]->fail = root;
                }
                else
                {
                    ans = nx->fail;
                    while(ans && ans->Next[i] == NULL)
                    {
                        ans = ans->fail;
                    }
                    if(ans == NULL) nx->Next[i]->fail = root;
                    else nx->Next[i]->fail = ans->Next[i];
                }
                Q.push(nx->Next[i]);
            }
        }
    }
}
void ac_automation(char *s)
{
    node* p = root;
    node* res;
    for(int i = 0; s[i]; i++)
    {
        int num;
        if(s[i] == 'E') num = 0;
        else if(s[i] == 'S') num = 1;
        else if(s[i] == 'W') num = 2;
        else num = 3;
        if(p->Next[num] == NULL)
        {
            while(p && p->Next[num] == NULL)
            {
                p = p->fail;
            }
            if(p == NULL) p = root;
            else
            {
                p = p->Next[num];
                res = p;
                while(res != root && !res->visit)
                {
                    res->flag = true;
                    res->visit = true;
                    res = res->fail;
                }
            }
        }
        else
        {
                p = p->Next[num];
                res = p;
                while(res != root && !res->visit)
                {
                    res->flag = true;
                    res->visit = true;
                    res = res->fail;
                }
        }
    }
}
int main()
{
//    freopen("C:\\Users\\creator\\Desktop\\in.txt","r",stdin ) ;
//    freopen("C:\\Users\\creator\\Desktop\\out.txt","w",stdout ) ;

    int T;
    scanf("%d", &T);
    int Ca = 1;
    while(T--)
    {
        cnt = 0;
        scanf("%s", KM);
        scanf("%d", &N);
        root = getV();
        root->d = 0;
        root->h = 0;
        root->flag = true;
        for(int i = 1; i <= N; i++)
        {
            scanf("%s", S[i]);
            Insert(S[i], i);
        }
        build_fail();
        ac_automation(KM);
        bfs();
//        for(int i = 1; i <= N; i++)
//        {
//            Search(S[i], i);
//        }
        printf("Case #%d:\n", Ca++);
        for(int i = 1; i <= N; i++)
        {
            printf("%d\n", re[i]->h);
        }
    }
    return 0;
}
内容概要:本文详细记录了对一个Android ARM64静态ELF文件中字符串加密机制的逆向分析过程。该ELF文件的所有字符串均被加密,无法通过常规strings命令或IDA直接识别。作者通过分析发现,加密字符串存储在.rodata段,其解密所需信息(包括密文地址、长度和16位密钥)保存在.data.rel.ro段的40字节描述符中。核心解密函数sub_10F408采用自反的双pass流密码算法,结合固定密钥KEY_TERM(由.data段24字节数据计算得出),实现字节级非线性、位置与长度相关的加密。文章还复现了完整的Python解密脚本,并揭示了该保护机制的本质为代码混淆而非强加密,最终成功批量解密全部956条字符串,暴露程序真实行为,如shell命令模板、设备标识篡改、网络重置等操作。此外,文中还提及未启用的自定义壳框架及其反dump设计。; 适合人群:具备逆向工程基础的安全研究人员、二进制分析人员及对ELF保护技术感兴趣的开发者。; 使用场景及目标:①学习ELF二进制中字符串加密的典型实现方式与逆向突破口;②掌握从结构识别、函数追踪到算法还原的完整逆向流程;③理解“绑定二进制”的完整性校验设计及其局限性;④实践编写IDAPython脚本自动化提取与解密敏感数据。; 阅读建议:此资源以实战案例驱动,不仅展示技术细节,更强调逆向思维与验证方法,建议读者结合IDA调试环境,逐步跟随文中步骤进行动态分析与算法验证,深入理解每一步的推理依据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值