[ACM] POJ 3096 Surprising Strings (use of map), poj3096

Source: Internet
Author: User

[ACM] POJ 3096 Surprising Strings (use of map), poj3096

Surprising Strings
Time Limit:1000 MS   Memory Limit:65536 K
Total Submissions:5783   Accepted:3792

Description

TheD-pairsOf a string of letters are the ordered pairs of letters that are distance D from each other. A string isD-uniqueIf all of its D-pairs are different. A string isSurprisingIf it is D-unique for every possible distance D.

Consider the string ZGBG. its 0-pairs are ZG, GB, and BG. since these three pairs are all different, ZGBG is 0-unique. similarly, the 1-pairs of ZGBG are ZB and GG, and since these two pairs are different, ZGBG is 1-unique. finally, the only 2-pair of ZGBG is ZG, so ZGBG is 2-unique. thus ZGBG is surprising. (Note that the fact that ZG is both a 0-pair and a 2-pair of ZGBG is irrelevant, because 0 and 2 are different distances .)

Acknowledgement:This problem is wrongly red by the "Puzzling Adventures" column in the December 2003 issueScientific American.

Input

The input consists of one or more nonempty strings of at most 79 uppercase letters, each string on a line by itself, followed by a line containing only an asterisk that signals the end of the input.

Output

For each string of letters, output whether or not it is surprising using the exact output format shown below.

Sample Input

ZGBGXEEAABAABAAABBBCBABCC*

Sample Output

ZGBG is surprising.X is surprising.EE is surprising.AAB is surprising.AABA is surprising.AABB is NOT surprising.BCBABCC is NOT surprising.

Source

Mid-Central USA 2006


Solution:

Judge whether a string is surpring.

Condition: All D-pairs of the string. D indicates the distance between two letters in the string. If all D-pairs are different, the string is D-unique. If all distance D is D-unique, the string is surpring.

For example, ZGBG

0-pairs: zg gb bg is not the same, it is 0-unique

1-pairs: zb gg is not the same, it is 1-unique

2-pairs: ZG is not the same, it is 2-unique

In summary, ZGBG is surpring

Each D-pairs is used for determination. If a D-pairs is the same, the string is certainly not surpring.

Map <string, bool> is used to determine whether a string has already appeared. Pay attention to its declared position and declare it in each layer of D loop.

Code:

# Include <iostream> # include <stdio. h> # include <map> # include <string. h> using namespace std; char str [80]; int main () {while (scanf ("% s", str )! = EOF & str [0]! = '*') {Int len = strlen (str); if (len <= 2) {cout <str <"is surprising. "<endl; continue;} bool OK = 1; // surpring for (int d = 0; d <= len-2; d ++) // The distance from d {bool dtap = 1; // is D-unique map <string, bool> mp; // pay attention to the declared position for (int s = 0; s <= len-2 & s + d + 1 <len; s ++) {string temp = ""; temp + = str [s]; temp + = str [s + d + 1]; if (mp [temp]) // This string has appeared {dtap = 0; break ;} else mp [temp] = 1;} if (! Dtap) {OK = 0; break;} if (OK) cout <str <"is surprising. "<endl; else cout <str <" is NOT surprising. "<endl;} return 0 ;}



What courses should I prepare for attending the ACM competition?

ACM common algorithms and exercises
Stage 1: practice common classical algorithms. Each of the following algorithms is used for 10 to 20 times, while streamlining the code by myself,
Because it is too common, you don't have to think about it when writing. You can even turn off the monitor within 10-15 minutes.
Come out.
1. Transient (Floyd, Dijstra, BellmanFord)
2. minimal spanning tree (write a prim first, and kruscal should use and query the set, which is hard to write)
3. Large numbers (High Precision) addition, subtraction, multiplication, and Division
4. Binary Search. (the code can be less than five lines)
5. Perform cross multiplication, determine the intersection of line segments, and then write a convex hull.
6. BFS, DFS, and skilled hash tables (be familiar, flexible, and concise)
7. mathematical formulas include: Moving and Division (within two rows), line segment intersection, and multi-angle area.
8. There are many tips for calling the system qsort.
9. Conversion between any binary systems

Stage 2: Exercise is a bit more complex, but it is also commonly used.
For example:
1. Bipartite Graph Matching (Hungary), minimum path Overwrite
2. network flow, minimum charge flow.
3. Line Segment tree.
4. Check the set.
5. Familiar with various typical dynamic planning models: LCS, longest incrementing substrings, triangle partitioning, and memory dp
6. classification algorithms. Game tree and binary method.
7. The largest group and the largest independent set.
8. points are located within the polygon.
9. Differential constraint system.
10. Bidirectional breadth search, A * algorithm, minimum dissipation first.

Related Knowledge

Graph Theory

Path Problems
0/1 Edge Weight Shortest Path
BFS
Non-negative edge weight Shortest Path (Dijkstra)
Features that can be solved by Dijkstra
Negative Edge Weight Shortest Path
Bellman-Ford
Bellman-Ford's Yen-s Optimization
Differential constraint system
Floyd
Generalized path
Passing Closure
Extremely small and extremely large distance/extremely small distance
Euler Path/Tour
Ring Algorithm
Euler Path/Tour of a hybrid Graph
Hamilton Path/Tour
Hamilton Path/Tour construction of a special Graph

Tree Generation
Minimum Spanning Tree
The k-th generation tree
Optimal Ratio Spanning Tree
0/1 score Planning
Degree limit Spanning Tree

Connectivity problems
Powerful DFS Algorithm
Undirected graph connectivity
Cut Point
Edge Cutting
2-connected branch
Directed Graph connectivity
Strongly Connected Branch
2-SAT
Minimum vertex Base

Directed Acyclic Graph
Topological sorting
Relationship between Directed Acyclic graphs and Dynamic Planning

Bipartite Graph Matching
Conversion between general graph problems and bipartite graph Problems
Max matching
Minimum path overwrite of a Directed Graph
0/1 minimum coverage of the matrix
Complete match
Optimal Match
Stable marriage

Network Flow Problems
Simple Features of the network stream Model and Its Relationship with Linear Planning
Max flow least cut Theorem
Maximum Flow Problems
... The remaining full text>

What are the problems with acm dynamic planning?

ACM common algorithms and exercises
Stage 1: practice common classical algorithms. Each of the following algorithms is used for 10 to 20 times, while streamlining the code by myself,
Because it is too common, you don't have to think about it when writing. You can even turn off the monitor within 10-15 minutes.
Come out.
1. Transient (Floyd, Dijstra, BellmanFord)
2. minimal spanning tree (write a prim first, and kruscal should use and query the set, which is hard to write)
3. Large numbers (High Precision) addition, subtraction, multiplication, and Division
4. Binary Search. (the code can be less than five lines)
5. Perform cross multiplication, determine the intersection of line segments, and then write a convex hull.
6. BFS, DFS, and skilled hash tables (be familiar, flexible, and concise)
7. mathematical formulas include: Moving and Division (within two rows), line segment intersection, and multi-angle area.
8. There are many tips for calling the system qsort.
9. Conversion between any binary systems

Stage 2: Exercise is a bit more complex, but it is also commonly used.
For example:
1. Bipartite Graph Matching (Hungary), minimum path Overwrite
2. network flow, minimum charge flow.
3. Line Segment tree.
4. Check the set.
5. Familiar with various typical dynamic planning models: LCS, longest incrementing substrings, triangle partitioning, and memory dp
6. classification algorithms. Game tree and binary method.
7. The largest group and the largest independent set.
8. points are located within the polygon.
9. Differential constraint system.
10. Bidirectional breadth search, A * algorithm, minimum dissipation first.

Related Knowledge

Graph Theory

Path Problems
0/1 Edge Weight Shortest Path
BFS
Non-negative edge weight Shortest Path (Dijkstra)
Features that can be solved by Dijkstra
Negative Edge Weight Shortest Path
Bellman-Ford
Bellman-Ford's Yen-s Optimization
Differential constraint system
Floyd
Generalized path
Passing Closure
Extremely small and extremely large distance/extremely small distance
Euler Path/Tour
Ring Algorithm
Euler Path/Tour of a hybrid Graph
Hamilton Path/Tour
Hamilton Path/Tour construction of a special Graph

Tree Generation
Minimum Spanning Tree
The k-th generation tree
Optimal Ratio Spanning Tree
0/1 score Planning
Degree limit Spanning Tree

Connectivity problems
Powerful DFS Algorithm
Undirected graph connectivity
Cut Point
Edge Cutting
2-connected branch
Directed Graph connectivity
Strongly Connected Branch
2-SAT
Minimum vertex Base

Directed Acyclic Graph
Topological sorting
Relationship between Directed Acyclic graphs and Dynamic Planning

Bipartite Graph Matching
Conversion between general graph problems and bipartite graph Problems
Max matching
Minimum path overwrite of a Directed Graph
0/1 minimum coverage of the matrix
Complete match
Optimal Match
Stable marriage

Network Flow Problems
Simple Features of the network stream Model and Its Relationship with Linear Planning
Max flow least cut Theorem
Maximum Flow Problems
... The remaining full text>

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.