最纯粹的Trie图题目
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
struct Node
{
Node* son[26];
Node* prev;
bool badNode;
Node()
{
memset(son, 0, sizeof(son));
prev = NULL;
badNode = false;
}
};
int ncount;
Node tree[200];
void insert(char * s)
{
Node* p = tree;
for (int i = 0; s[i] != '\0'; i++)
{
if (p->son[s[i] - 'a'] == NULL)
p->son[s[i] - 'a'] = tree + ncount++;
p = p->son[s[i] - 'a'];
}
p->badNode = true;
}
void buildDfa()
{
deque<Node*> q;
for (int i = 0; i < 26; i++)
{
if (tree[0].son[i])
{
tree[0].son[i]->prev = tree;
q.push_back(tree[0].son[i]);
}
}
while (!q.empty())
{
Node* p = q.front();
q.pop_front();
for (int i = 0; i < 26; i++)
{
Node *p2 = p->son[i];
if (p2)
{
Node * prev = p->prev;
while (prev)
{
if (prev->son[i])
{
p2->prev = prev->son[i];
if (prev->son[i]->badNode)
p2->badNode = true;
break;
}
prev = prev->prev;
}
if (prev == NULL)
p2->prev = tree;
q.push_back(p2);
}
}
}
}
bool searchDfa(char *s)
{
Node *p = tree;
for (int i = 0; s[i] != '\0'; i++)
{
while (p)
{
if (p->son[s[i] - 'a'])
{
p = p->son[s[i] - 'a'];
if (p->badNode)
return 1;
break;
}
p = p->prev;
}
if (p == NULL)
p = tree;
}
return false;
}
int main()
{
ncount = 1;
int n, m;
char str[100];
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
{
scanf("%s", str);
insert(str);
}
buildDfa();
for (int i = 0; i < m; i++)
{
scanf("%s", str);
if (searchDfa(str))
printf("1\n");
else
printf("0\n");
}
}
POJ 3987 病毒查找
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
#include<ctype.h>
#include<math.h>
using namespace std;
struct Node
{
Node* last;
bool isCalc;
int whichWord;
Node *prev, *son[26];
};
Node tree[250 * 1000 + 100];
queue<Node*> q;
int ncount;
char programStr[5100010];
int ansCount;
int visited[250 * 1000 + 100];
int n;
inline Node* newnode()
{
tree[ncount].last = NULL;
tree[ncount].isCalc = false;
tree[ncount].prev = NULL;
tree[ncount].whichWord = -1;
for (int i = 0; i < 26; i++)
{
tree[ncount].son[i] = NULL;
}
return tree + ncount++;
}
inline void insert(char * s, int whichWord)
{
Node* p = tree;
for (int i = 0; s[i] != '\0'; i++)
{
if (p->son[s[i] - 'A'] == NULL)
p->son[s[i] - 'A'] = newnode();
p = p->son[s[i] - 'A'];
}
p->whichWord = whichWord;
}
void buildDfa()
{
for (int i = 0; i < 26; i++)
{
if (tree[0].son[i])
{
tree[0].son[i]->prev = tree;
q.push(tree[0].son[i]);
}
}
while (!q.empty())
{
Node* p = q.front();
q.pop();
for (int i = 0; i < 26; i++)
{
if (p->son[i])
{
Node *prev = p->prev;
while (prev)
{
if (prev->son[i])
{
p->son[i]->prev = prev->son[i];
if (prev->son[i]->whichWord!=-1)
{
p->son[i]->last = prev->son[i];
}
else if (prev->son[i]->last)
{
p->son[i]->last= prev->son[i]->last;
}
break;
}
else
prev = prev->prev;
}
if (prev == NULL)
p->son[i]->prev = tree;
q.push(p->son[i]);
}
}
}
}
int strToInt(char *s, int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += (s[i] - '0')*(int)pow(10, n - i - 1);
}
return sum;
}
inline void foo(Node * &p, const char &ch)
{
while (p)
{
if (p->son[ch - 'A'])
{
p = p->son[ch - 'A'];
break;
}
else
p = p->prev;
}
if (p == NULL)
p = tree;
if (p->last && p->last->isCalc == false)
{
Node * temp = p->last;
while (temp)
{
temp->isCalc = true;
if(visited[temp->whichWord]==0)
{
ansCount++;
visited[temp->whichWord]=1;
}
temp = temp->last;
}
}
if (p->whichWord != -1 && p->isCalc == false)
{
p->isCalc = true;
if(visited[p->whichWord]==0)
{
ansCount++;
visited[p->whichWord]=1;
}
}
}
void searchDfa1(char *s)
{
Node *p = tree;
for (int i = 0; s[i] != '\0'; i++)
{
if (s[i] == '[')
{
int cnt = 0;
i++;
int i2 = i;
while (isdigit(s[i]))
{
i++;
cnt++;
}
int q = strToInt(s + i2, cnt);
while (q--)
{
foo(p, s[i]);
}
i++;
}
else
{
foo(p, s[i]);
}
}
}
void searchDfa2(char *s)
{
Node *p = tree;
int len = strlen(s);
for (int i = len - 1; i >= 0; i--)
{
if (s[i] == ']')
{
int cnt = 0;
i--;
char x = s[i];
i--;
while (isdigit(s[i]))
{
i--;
cnt++;
}
int q = strToInt(s + i + 1, cnt);
while (q--)
{
foo(p, x);
}
}
else
{
foo(p, s[i]);
}
}
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
memset(visited,0,sizeof(visited));
ncount = 0;
ansCount = 0;
newnode();//初始化根节点
scanf("%d", &n);
char str[1010];
for (int i = 0; i < n; i++)
{
scanf("%s", str);
insert(str, i);
}
scanf("%s", programStr);
buildDfa();
searchDfa1(programStr);
searchDfa2(programStr);
printf("%d\n", ansCount);
}
}
POJ 1204 字谜
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
#define INF 1001
struct Node
{
int whichWord;
Node *prev, *son[26];
};
Node tree[INF*INF];
queue<Node*> q;
struct Location
{
int r, c, d;
};
Location ans[1001];
int r, c, w, ncount;
char graph[INF][INF], tp[INF];
int dx[8] = { -1,-1,0,1,1,1 ,0 ,-1 };
int dy[8] = { 0 ,1 ,1,1,0,-1,-1,-1 };
Node* newnode()
{
tree[ncount].prev = NULL;
tree[ncount].whichWord = -1;
for (size_t i = 0; i < 26; i++)
{
tree[ncount].son[i] = NULL;
}
return tree + ncount++;
}
void insert(char * s, int whichWord)
{
Node* p = tree;
int len = strlen(s);
for (int i = len - 1; i >= 0; i--)
{
if (p->son[s[i] - 'A'] == NULL)
p->son[s[i] - 'A'] = newnode();
p = p->son[s[i] - 'A'];
}
p->whichWord = whichWord;
}
void buildDfa()
{
Node *p = tree;
for (int i = 0; i < 26; i++)
{
if (p->son[i])
{
p->son[i]->prev = tree;
q.push(p->son[i]);
}
}
while (!q.empty())
{
p = q.front();
q.pop();
for (int i = 0; i < 26; i++)
{
if (p->son[i])
{
Node *prev = p->prev;
while (prev)
{
if (prev->son[i])
{
p->son[i]->prev = prev->son[i];
break;
}
else
prev = prev->prev;
}
if (prev == NULL)
p->son[i]->prev = tree;
q.push(p->son[i]);
}
}
}
}
void searchDfa(int rr, int cc, int kk)
{
int nx = rr;
int ny = cc;
Node *p = tree;
for (; 0 <= nx && nx < r && 0 <= ny && ny < c; nx += dx[kk], ny += dy[kk])
{
while (p)
{
if (p->son[graph[nx][ny] - 'A'])
{
p = p->son[graph[nx][ny] - 'A'];
break;
}
else
p = p->prev;
}
if (p == NULL)
p = tree;
Node* temp = p;
while (temp != tree)
{
if (p->whichWord != -1)
{
ans[temp->whichWord].r = nx;
ans[temp->whichWord].c = ny;
ans[temp->whichWord].d = kk;
//temp->whichWord = -1;
}
temp = temp->prev;
}
}
}
void search()
{
for (int i = 0; i < c; i++)
{
searchDfa(0, i, 5);
searchDfa(0, i, 4);
searchDfa(0, i, 3);
searchDfa(r - 1, i, 7);
searchDfa(r - 1, i, 0);
searchDfa(r - 1, i, 1);
}
for (int i = 0; i < r; i++)
{
searchDfa(i, 0, 1);
searchDfa(i, 0, 2);
searchDfa(i, 0, 3);
searchDfa(i, c - 1, 7);
searchDfa(i, c - 1, 6);
searchDfa(i, c - 1, 5);
}
}
int main()
{
while (scanf("%d%d%d", &r, &c, &w) == 3)
{
ncount = 1;
for (int i = 0; i < r; i++)
scanf("%s", graph[i]);
for (int i = 0; i < w; i++)
{
scanf("%s", tp);
insert(tp, i);
}
buildDfa();
search();
for (int i = 0; i < w; i++)
{
printf("%d %d %c\n", ans[i].r, ans[i].c, 'A' + (ans[i].d + 4) % 8);
}
}
}
POJ 3691 DNA repair
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
#include<ctype.h>
#include<math.h>
using namespace std;
int chge(char ch)
{
switch (ch)
{
case 'A':return 0;
case 'C':return 1;
case 'G':return 2;
case 'T':return 3;
default:
exit(1);
}
}
struct Node
{
int prev;
int child[4];
bool badNode;
};
Node tree[20 * 50 + 100];
queue<int> q;
int ncount;
int n;
char pstr[25];
char str[1010];
int dp[1000 + 10][20 * 50 + 100];
int newnode()
{
tree[ncount].badNode = false;
tree[ncount].prev = -1;
for (size_t i = 0; i < 4; i++)
{
tree[ncount].child[i] = -1;
}
return ncount++;
}
void insert()
{
int p = 0;
for (int i = 0; pstr[i] != '\0'; i++)
{
if (tree[p].child[chge(pstr[i])] == -1)
{
tree[p].child[chge(pstr[i])] = newnode();
}
p = tree[p].child[chge(pstr[i])];
}
tree[p].badNode = true;
}
void buildDfa()
{
for (int i = 0; i < 4; i++)
{
if (tree[0].child[i] != -1)
{
tree[tree[0].child[i]].prev = 0;
q.push(tree[0].child[i]);
}
else
{
tree[0].child[i] = 0;
}
}
while (!q.empty())
{
int p = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
if (tree[p].child[i] != -1)
{
int prev = tree[p].prev;
while (prev != -1)
{
if (tree[prev].child[i] != -1)
{
tree[tree[p].child[i]].prev = tree[prev].child[i];
if (tree[tree[prev].child[i]].badNode)
tree[tree[p].child[i]].badNode = true;
break;
}
else
prev = tree[prev].prev;
}
if (prev == -1)
{
tree[tree[p].child[i]].prev = 0;
}
q.push(tree[p].child[i]);
}
else
{
tree[p].child[i] = tree[tree[p].prev].child[i];
}
}
}
}
int main()
{
int caseCnt = 1;
while (scanf("%d", &n) == 1 && n != 0)
{
ncount = 0;
newnode();
for (int i = 0; i < 1000 + 10; i++)
for (int j = 0; j < 20 * 50 + 100; j++)
dp[i][j] = 1 << 30;
for (int i = 0; i < n; i++)
{
scanf("%s", pstr);
insert();
}
scanf("%s", str);
buildDfa();
int len = strlen(str);
dp[0][0] = 0;
for (int i = 1; i <= len; i++)
{
for (int j = 0; j < ncount; j++)
{
if (dp[i - 1][j] != 1 << 30)
{
for (int k = 0; k < 4; k++)
{
if (tree[tree[j].child[k]].badNode == false)
{
if (chge(str[i - 1]) == k)
dp[i][tree[j].child[k]] = min(dp[i][tree[j].child[k]], dp[i - 1][j]);
else
dp[i][tree[j].child[k]] = min(dp[i][tree[j].child[k]], dp[i - 1][j] + 1);
}
}
}
}
}
int ans = 1 << 30;
for (int i = 0; i < ncount; i++)
if (tree[i].badNode == false && ans>dp[len][i])
{
ans = dp[len][i];
}
printf("Case %d: ", caseCnt++);
if (ans == 1 << 30)
printf("-1\n");
else
printf("%d\n", ans);
}
}
POJ 1625 不包含单词的语句个数
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<queue>
#include<ctype.h>
#include<map>
#include<math.h>
using namespace std;
struct Node
{
int prev;
int child[55];
bool badNode;
};
Node tree[10 * 50 + 100];
queue<int> q;
int ncount;
map<char, int> charToInt;
int acount;
int n, m, p;
char pstr[15];
int newnode()
{
tree[ncount].badNode = false;
tree[ncount].prev = -1;
for (int i = 0; i < n; i++)
{
tree[ncount].child[i] = -1;
}
return ncount++;
}
void insert()
{
int p = 0;
for (int i = 0; pstr[i] != '\0'; i++)
{
if (tree[p].child[charToInt[pstr[i]]] == -1)
{
tree[p].child[charToInt[pstr[i]]] = newnode();
}
p = tree[p].child[charToInt[pstr[i]]];
}
tree[p].badNode = true;
}
void buildDfa()
{
for (int i = 0; i < n; i++)
{
if (tree[0].child[i] != -1)
{
tree[tree[0].child[i]].prev = 0;
q.push(tree[0].child[i]);
}
else
{
tree[0].child[i] = 0;
}
}
while (!q.empty())
{
int p = q.front();
q.pop();
for (int i = 0; i < n; i++)
{
if (tree[p].child[i] != -1)
{
int prev = tree[p].prev;
while (prev != -1)
{
if (tree[prev].child[i] != -1)
{
tree[tree[p].child[i]].prev = tree[prev].child[i];
if (tree[tree[prev].child[i]].badNode)
tree[tree[p].child[i]].badNode = true;
break;
}
else
prev = tree[prev].prev;
}
if (prev == -1)
{
tree[tree[p].child[i]].prev = 0;
}
q.push(tree[p].child[i]);
}
else
{
tree[p].child[i] = tree[tree[p].prev].child[i];
}
}
}
}
struct BigInteger
{
int A[25];
enum
{
MOD = 10000
};
BigInteger()
{
memset(A, 0, sizeof(A)); A[0] = 1;
}
void set(int x)
{
memset(A, 0, sizeof(A)); A[0] = 1; A[1] = x;
}
void print()
{
printf("%d", A[A[0]]);
for (int i = A[0] - 1; i > 0; i--)
{
if (A[i] == 0)
{
printf("0000"); continue;
}
for (int k = 10; k*A[i] < MOD; k *= 10) printf("0");
printf("%d", A[i]);
}
printf("\n");
}
int& operator [] (int p)
{
return A[p];
}
const int& operator [] (int p) const
{
return A[p];
}
BigInteger operator + (const BigInteger& B)
{
BigInteger C;
C[0] = max(A[0], B[0]);
for (int i = 1; i <= C[0]; i++)
C[i] += A[i] + B[i], C[i + 1] += C[i] / MOD, C[i] %= MOD;
if (C[C[0] + 1] > 0) C[0]++;
return C;
}
BigInteger operator * (const BigInteger& B)
{
BigInteger C;
C[0] = A[0] + B[0];
for (int i = 1; i <= A[0]; i++)
for (int j = 1; j <= B[0]; j++)
{
C[i + j - 1] += A[i] * B[j], C[i + j] += C[i + j - 1] / MOD, C[i + j - 1] %= MOD;
}
if (C[C[0]] == 0) C[0]--;
return C;
}
};
int main()
{
int caseCnt = 1;
while (scanf("%d%d%d", &n, &m, &p) == 3)
{
ncount = 0;
newnode();
acount = 0;
BigInteger dp[51][101];
dp[0][0].set(1);
scanf("%s", pstr);
for (int i = 0; pstr[i] != '\0'; i++)
{
charToInt[pstr[i]] = acount++;
}
for (int i = 0; i < p; i++)
{
scanf("%s", pstr);
insert();
}
buildDfa();
for (int i = 1; i <= m; i++)
{
for (int j = 0; j < ncount; j++)
{
for (int k = 0; k < n; k++)
{
if (tree[tree[j].child[k]].badNode == false)
{
dp[i][tree[j].child[k]] = dp[i][tree[j].child[k]] + dp[i - 1][j];
}
}
}
}
BigInteger ans;
for (int i = 0; i < ncount; i++)
{
ans = ans + dp[m][i];
}
ans.print();
}
}
The Book List
#include<stdio.h>
#include<map>
#include<string>
#include<iostream>
#include<set>
using namespace std;
struct Node
{
map<string, int> category;
set<string> book;
};
Node tree[1508];
int ncount;
string address;
void Insert(int cur, int root)
{
int i = cur;
string word;
while (i < address.size() && address[i] != '/')
{
word += address[i];
i++;
}
if (address[i] == '/')//category
{
if (tree[root].category.count(word) == 0)//不存在这个目录,新建节点
{
tree[root].category[word] = ++ncount;
}
Insert(i + 1, tree[root].category[word]);
}
else//book
{
tree[root].book.insert(word);
}
}
void showSpace(int n)
{
for (int i = 0; i < n; i++)
printf(" ");
}
void show(int depth, int root)
{
for (auto cate : tree[root].category)
{
showSpace(depth - 1);
printf("%s\n", cate.first.c_str());
show(depth + 1, cate.second);
}
for (auto b : tree[root].book)
{
showSpace(depth - 1);
printf("%s\n", b.c_str());
}
}
void init()
{
for (int i = 0; i <= ncount; i++)
{
tree[i].book.clear();
tree[i].category.clear();
}
ncount = 0;
}
int main()
{
int caseCnt = 1;
ncount = 0;
//freopen("a.txt", "w", stdout);
while (getline(cin, address))
{
do
{
if (address == "0")
{
printf("Case %d:\n", caseCnt++);
show(1, 0);
init();
}
else
Insert(0, 0);
} while (getline(cin, address));
return 0;
}
}