< from the original blog > cute string algorithms

Source: Internet
Author: User

In the very strong and very concerned about the study of the younger brother learns the Penguin senior Abnormal exam, we choose to learn a new posture
First:KMP algorithm
This is a little Dee more blog algorithm, I am embarrassed here caught dead, so offer friends chain one: http://rabbithu.xyz/index.php?title=2017-04-01-01

Second:trie tree (dictionary tree)
Boingonium hindering, this is my first class in the Oi group!? (Although everyone is very decadent, but the simplicity of the algorithm is beyond doubt!) )
In the question of string, we will encounter some sub-string ah ~ prefix Ah ~ problem, if the normal enumeration traversal of the complexity of O (n*m) (n, m for string length), obviously, this is very slow! What about that?
At this time, there is a very clever person, think of the tree! But what is the relationship between a tree and a string? Look at the picture and you know it!

In this tree, the side represents the letter, the node store is the end of a word, so that we can be stored in a 26-fork tree Any word we want w!
"So, the root node represents an empty string"

There must have been some people thinking about how the code was implemented, before the chain? Obviously, if we store it in front of the chain, we need to traverse the current letter at each level, whether it's inserting or querying, so does the complexity explode? So we use a simple and witty way to achieve the process of saving a son!

struct Node{int data;int childs[26];} Tree[n];

Trie nodes can be stored using a struct, in the following code, Trans[i] indicates the son node number to which the transfer edge of the character on the edge of the node is reached, and if 0 means no son. is not special meow Ah!

Insert : If a string is inserted in a tree with a string set as lowercase s In fact we just use the complexity of the O (Len), one at a time to the letter edge (check if there is this edge exists, then directly next, otherwise built edge), and in the last mark is the end of the word is good!
void Insert () {int l=strlen (num), now=1;for (int i=0;i<l;i++) {if (!tree[now].childs[num[i]-' a ']) tree[now].childs[ num[i]-' 0 ']=tot++;now=tree[now].childs[num[i]-' a ';} Tree[now].end=1;return;}

Query : Queries whether a string S is the prefix of a string in a given set of strings: What's there to say? No, it's the same as inserting it! If not this side directly return false is good ah "no code, whined!" "Finally send an example: POJ 3630//in which the insertion, query and simultaneous complexity optimization, easy to understand

#include <cstdio> #include <cstring> #define M 11using namespace Std;const int N=1e5+5;int N,t,tot,ans;char Num[m];struct Hhh{bool end;int childs[m];void Clear () {memset (childs,0,sizeof); childs;}} Tree[n];bool Insert () {int l=strlen (num), now=1;for (int i=0;i<l;i++) {if (!tree[now].childs[num[i]-' 0 ']) {Tree[now] . childs[num[i]-' 0 ']=tot++;tree[tot-1].clear ();} else if (I==L-1)  return 0;now=tree[now].childs[num[i]-' 0 '];if (tree[now].end)  return 0;} Tree[now].end=1;return 1;} int main () {scanf ("%d", &t), while (t--) {tot=1;ans=1;tree[tot++].clear (); scanf ("%d", &n) and while (n--) {scanf ("% S ", num); if (!insert ()) ans=0;} if (ans) printf ("yes\n"), Else printf ("no\n");} return 0;}

If you have a small partner to see here, then you can go to try poj2001 if it is not wrong, you can only use pointers to implement the Trie tree code as follows:

 #include <cstdio> #include <cstring> #define N 1111#define M      22using namespace Std;int N,i;char ha[n][m];struct node{int count;      Node *childs[26];          Node () {count=0;          int i;      for (i=0;i<26;i++) childs[i]=null; }};node *root = new Node;node *now,*newnode;void Insert (int x) {int l=strlen (ha[x]); now=root;for (int j=0;j<l;j++) {if (now->childs[ha[x][j]-' a ']) {now=now->childs[ha[x][j]-' a '];++ (Now->count);} else {newnode=new node;++ (newnode->count); now->childs[ha[x][j]-' a ']=newnode;now=newnode;}} return;} void search (int x) {Now=root;int l=strlen (ha[x]), Cnt=0;char ans[m];for (int j=0;j<l;j++) {now=now->childs[ha[x][ j]-' a '];ans[cnt++]=ha[x][j];ans[cnt]= ' + '; if (now->count==1) {printf ("%s%s\n", Ha[x],ans); return;}} printf ("%s%s\n", Ha[x],ans); return;} int main () {while (~SCANF ("%s", Ha[i])) inserts (i++); for (int j=0;j<i;j++) search (j); return 0;} 

I don't think anyone will see here ... Here are 20160603 analog Easy round1 T2 shocked

Description "Shocked, oier stay up late to learn to be persistent tire tree is because ..."--Penguin headlines francium Penguins heard oh El to join the Penguin headlines, write legendary news, won the 2333 News Prize of the Pulitzer Goose. Let him go to shock department to experience a bit. Shock there are some different ways of writing, all made up of lowercase letters and not exceeding 100100 in length. such as: Shock,choc,schock and so on. Each type of shock has different weights in the shock department. Given the initial weight of an nn type shock, and a mm operation. There are two types of operations: 1. Modification: Give a shocking writing, if this writing exists, then the weight of this shocking writing is modified to XX. 2. Inquiry: Before the xx operation, some kind of shocking the weight of the wording. Even if is currently the second DD operation, then asks the first d?xd?x operation, the weight of this kind of shock notation. Note: The subject is forced online, please note the input output format input the first line of two integer nn, mm. The next nn line is a string SS for each line, and an integer XX, respectively, representing a shocking notation and the initial weights of the notation. The next mm line is a string SS and an integer xx per line. The string SS represents a shocking notation. If this operation is modified, XX will change the weight of this shocking notation to XX. If the operation is to inquire, XX representative asked XX times before the operation, the weight of the shock notation. The subject is forced on-line, if the last operation of the output of Er or Shock, then on behalf of the operation for the inquiry, otherwise the operation for the modification. The 11th operation is specified as modification. Output outputs a total of mm rows. Each row corresponds to one operation. For each set of modifications, if the modification succeeds (i.e. there is a corresponding shock notation), output Ac. If there is no corresponding shock notation, output Er. For each group of inquiries, if there is a corresponding shock writing, output corresponding to XX times before the operation, some kind of shock writing weights XX. If there is no corresponding shock notation, output Shock.

The puzzle: temporary cutting

AC Code:

#include <cstdio> #include <cstring> #include <vector> #include <algorithm> #define M 1111# Define N 11111#define inf 1111111using namespace std;int n,m,x,b,pre,j;char a[m];struct ha{int Time,dt;bool operator < (Ha j) Const{return Time<j.time;} Ha () {time=inf;dt=0;}}; struct Hhh{vector 

< from the original blog > cute string algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.