Upload
s-mahbub-uz-zaman
View
395
Download
0
Embed Size (px)
Citation preview
ACMACMACMACMACMACMACMA
CMACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
MACMACMACMACMACMACMAC
Mtyuiopasdfghjklzxcvbnmqwert
ACM Training Session
BRAC UNIVERSITY
3/8/2012
Raduan & Mahbub
This work is licensed under a Creative Commons Attribution-NonCommercial-
ShareAlike 3.0 Unported License.
Page 1 of 6
Raduanul Islam (10101024) & S. Mahbub – Uz – Zaman (09301004)
String (Java)
Frequently Asked Questions (FAQ)
1. What is String?
A string is a sequence of characters treated as a single unit. A string may include letters,
digits and various special characters, such as +, -, *, / and $. A string is an object of
class String. [For more info read Chapter 29 of Java How to Program]
2. Is String a primitive data type in Java?
No String is not a primitive data type in Java, even though it is one of the most
extensively used object. Strings in Java are instances of String class.
[http://docs.oracle.com/javase/tutorial/java/data/strings.html]
3. Is the + operator overloaded?
Java does not have operator overloading, but string concatenation is a special case. When
the + operator is applied to a String, the two values are appended as a new String. If you
use the + operator with a String and a primitive value, such as an int or long, the Java
interpreter implicitly converts the primitive value a string representation and concatenates
them.
4. How can you say the + operator is not overloaded in Java!
The plus operator for Java strings is a limited exceptional case that is practically the same
as operator overloading. It’s a fine distinction but using + to append object contents is not
a general case in Java, it only applies to String objects. An equivalent append operation
may be meaningful and useful for data storage types but does not have a general
application and is not implemented in Java.
// Compiler error: "operator + cannot be applied to java.util.Vector"
// Vector vector = new Vector() + new Vector();
Page 2 of 6
Raduanul Islam (10101024) & S. Mahbub – Uz – Zaman (09301004)
Classes that you will need for ACM
1. String (http://docs.oracle.com/javase/6/docs/api/java/lang/String.html)
2. StringTokenizer (http://docs.oracle.com/javase/6/docs/api/java/util/StringTokenizer.html)
3. StringBuffer (docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html)
4. Character (docs.oracle.com/javase/6/docs/api/java/lang/Character.html)
5. Matcher (http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html)
6. Pattern (http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)
// you have to learn some basic methods provided by these classes like
trim(), length(), toUpperCase() etc
The following topics will be needed for ACM
1. String Matching (Knuth Morris Pratt (KMP), Rabin – Karp, Aho – Corasick, Boyer –
Moore, Suffix Tree/Trie/Array, Finite State Automata)
2. Regular Expressions
3. String Permutation (SEPA)
4. Determine Subsequence
5. Determine Anagram
6. Determine Palindrome (check palindrome or make palindrome [DP])
7. Longest common Subsequence (DP)
8. Longest common Substring and Longest repeated Substring
9. Longest common prefix
10. Shortest common Superstring
11. String Alignment (Edit/Levenshetin Distance) (DP)
12. Hashing
13. Burrows wheeler transform
14. Radix Tree
15. Suffix automaton
Page 3 of 6
Raduanul Islam (10101024) & S. Mahbub – Uz – Zaman (09301004)
Some Common Definitions
1. Anagram: A word or phrase formed by reordering the letters of another word or phrase,
such as satin to stain.
2. Permutation: You are given two strings A and B. B will be said the permutation of A, if
you can make B from A by re-arranging some (or none) characters.
aabc is a permutation of caba
3. Substring: A substring (or factor) of a string T = t1…..tn is a string ̂ = t1+i…..tm+i , where
0 i and m + i n . A substring of a string is a prefix of a suffix of the string, and
equivalently a suffix of a prefix. If ̂ is a substring of T, it is also a subsequence, which is
a more general concept.
Given a pattern P, you can find its occurrences in a string T with a string searching
algorithm.
Finding the longest string which is equal to a substring of two or more strings is known
as the longest common substring problem.
Example: The string ana is equal to substrings (and subsequences) of banana at two
different offsets:
banana
|||||
ana||
|||
ana
Substrings are also called subwords (in America) or factors (in Europe). It is also a
subsequence
4. Subsequence: You are given two strings A and B. B will be said the subsequence of A, if
you can make B from A by deleting some (or none) characters. For example, ABD is a
subsequence of ABCDEF. Where, order of the elements is preserved.
5. Prefix: A prefix of a string T = t1…..tn is a string ̂ = t1…..tm, where m n. A
proper prefix of a string is not equal to the string itself (0 ); some sources
in
addition restrict a proper prefix to be non-empty (0 < m < n). A prefix can be seen as
a special case of a substring.
Example: The string ban is equal to a prefix (and substring and subsequence) of the string
banana:
banana
|||
Ban
A B C D E F
A B C D E F
Page 4 of 6
Raduanul Islam (10101024) & S. Mahbub – Uz – Zaman (09301004)
6. Suffix: A suffix of a string T = t1…..tn is a string ̂ = tn – m + 1…..tm where m n. A
proper suffix of a string is not equal to the string itself (0 ); again, a more
restricted interpretation is that it is also not empty (0 < m < n). A suffix can be seen as a
special case of a substring.
Example: The string nana is equal to a suffix (and substring and subsequence) of the
string banana:
banana
||||
nana
7. Border:
A border is suffix and prefix of the same string, e.g. "bab" is a border of "babab".
Different Characters:
Uppercase Letters: A, B, C, D, E
Lowercase Letters: a, b, c, d, e
Digits: 0, 1, 2, 3, 4
Punctuation: comma (,), full stop/period (.)
Control/non-printing/escape characters: carriage return, tab, newline
Page 5 of 6
Raduanul Islam (10101024) & S. Mahbub – Uz – Zaman (09301004)
Sample I/O
// Test Case Style
2
Runway
Tintin
// EOF Style
CSE
ECE
EEE
null (End Of File)
// Blank Line Style
Apple
Microsoft
Yahoo
(Blank Line)
// Reading A String after Reading an Integer Problem
1
Ki holo
/* nextInt() does not change the line so after reading 1; when we hit enter
causes the endOfLine (\r\n). This EOL is read by nextLine() and that’s why
“Ki holo” never read by nextLine() */
[http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html]
// Sentinel Style
Mili
Micro
Nano
Stop (Sentinel)
// Mixed
3
X 2
Y 3
Z 5
Page 6 of 6
Raduanul Islam (10101024) & S. Mahbub – Uz – Zaman (09301004)
Important Links
1. http://www.leepoint.net/notes-java/data/expressions/22compareobjects.html
Resources:
1. http://en.wikipedia.org/wiki/Wiki
2. http://www.codestyle.org
3. http://www.thefreedictionary.com