UVA-11604 General Sultan

Source: Internet
Author: User

Main topic:

There are several pattern strings that stitch together some pattern strings (one can be used multiple times) to form a long pattern string that determines whether two or more different spellings can be spelled into the same pattern string.

Ideas:

Magical composition, the solution of violence.

It can be found that if there are different spellings, the prefix of a pattern string should be the same as the suffix of a pattern string.

So we turned the problem into: starting with the two pattern strings, we kept matching the prefix to the front and back, and ended up with two strings at one point at a time.

So, each character of each string is treated as a point, and the n2len2 of the violent enumeration I string from Z to the suffix and J string (you can also, but do not let the prefix is itself) to match the prefix to see if one of the strings can be matched.

When (I,z) and J match, if I end first, then the point of (I,Z) will be connected to the first point of the J string mismatch, indicating that the next match should be from that position of J when the suffix looks for another prefix; if J ends first, it is similar to the previous one;

Finally, DFS is used to determine whether the first point of a pattern string can start at the end.

Ps:1, fromhttp://blog.csdn.net/houserabbit/article/details/38943645

2, the meaning of the array, the scope to be clear, it took me one hours to find that the array is small.

Code:

1#include <cstdio>2#include <cstring>3 using namespacestd;4 Const intm=109, n= -;5 intn,e,t,i,j,k,z,cnt,v[m*m*n*n],id[m][n],nex[m*m*n*n],len[m],head[m*N];6 BOOLvis[m*N],flag;7 CharS[m][n];8 9 voidAddintXintY) {v[++cnt]=y,nex[cnt]=head[x],head[x]=CNT;}Ten  One voidDfsintx) A { -     if(x==e) {flag=1;return; } -vis[x]=1; the      for(intI=head[x];~i;i=Nex[i]) -         if(!Vis[v[i]]) -         { - DFS (V[i]); +             if(flag)return; -         } + } A  at intMain () - { -      while(~SCANF ("%d",&N)) -     { -         if(!n) Break; -e=cnt=flag=0; in          for(i=1; i<=n;++i) -         { toscanf"%s%s", S[i],s[i]); +len[i]=strlen (S[i]); -              for(j=0; j<len[i];++j) id[i][j]=++e; the         } *          for(++e,i=0; i<=e;++i) head[i]=-1; $          for(i=1; i<=n;++i)Panax Notoginseng              for(z=0; z<len[i];++z) -                  for(j=1; j<=n;++j) the                 { +                     if(I==j &&!z)Continue; A                      for(k=0; K<len[j] && z+k<len[i];++k) the                         if(S[i][z+k]!=s[j][k]) Break; +                     if(Z+k==len[i] && k==Len[j]) Add (id[i][z],e); -                     Else if(K==len[j]) Add (id[i][z],id[i][z+K]); $                          Else if(z+k==Len[i]) Add (Id[i][z],id[j][k]); $                 } -          for(i=0; i<=e;++i) vis[i]=0; -          for(i=1; i<=n;++i) the         { -             if(!vis[id[i][0]]) DFS (id[i][0]);Wuyi             if(flag) Break; the         } -printf"Case #%d:",++t); Wu         if(flag) puts ("ambiguous.");ElsePuts"Not ambiguous."); -     } About     return 0; $}

UVA-11604 General Sultan

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.