Hiển thị các bài đăng có nhãn Anagram. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Anagram. Hiển thị tất cả bài đăng

Thứ Sáu, 11 tháng 1, 2019

UVa 10098 - Generating Fast, Sorted ...(very similar to UVa 195)

Link:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=1039
Sol:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool string_comp(const char& lhs, const char& rhs)
{
if (lhs >= 'A')
{
return (rhs < 'A' || lhs < rhs);
}
else
{
return (rhs < 'A' && lhs < rhs);
}
}
int main()
{
int T;
cin >> T;
string word;
while (T--)
{
cin >> word;
sort(word.begin(), word.end(), string_comp);
do
{
cout << word << '\n';
} while (next_permutation(word.begin(), word.end(), string_comp));
cout << '\n';
}
}

UVa 00642 - Word Amalgamation (go through the given small dictionary for the list of possible anagrams )

Link: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=8&page=show_problem&problem=583
Sol:
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 583 (642 - Word Amalgamation) */
/* SUBMISSION: 10094114 */
/* SUBMISSION TIME: 2012-05-09 19:14:40 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa00642_WordAmalgamation {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Map<String, String> dict = new HashMap<String, String>();
String word;
while (!(word = in.readLine()).equals("XXXXXX")) {
char[] ch = word.toCharArray();
Arrays.sort(ch);
dict.put(word, String.valueOf(ch));
}
while (!(word = in.readLine()).equals("XXXXXX")) {
Set<String> matches = new TreeSet<String>();
for (String s : dict.keySet()) {
char[] ch = word.toCharArray();
Arrays.sort(ch);
String s2 = String.valueOf(ch);
if (s2.equals(dict.get(s)))
matches.add(s);
}
if (!matches.isEmpty())
for (String s : matches)
System.out.println(s);
else
System.out.println("NOT A VALID WORD");
System.out.println("******");
}
in.close();
System.exit(0);
}
}

UVa 00630 - Anagram(II) (ad hoc, string)

Link: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=8&page=show_problem&problem=571
Sol:
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 571 (630 - Anagrams (II)) */
/* SUBMISSION: 09976734 */
/* SUBMISSION TIME: 2012-04-10 18:48:39 */
/* LANGUAGE: 2 */
import java.util.*;
import java.io.*;
public class UVa00630_AnagramsII {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int M = Integer.parseInt(in.readLine());
boolean first = true;
while (M-- > 0) {
in.readLine();
int N = Integer.parseInt(in.readLine());
String[] dict = new String[N];
for (int i = 0; i < N; ++i)
dict[i] = in.readLine();
if (first)
first = false;
else
System.out.println();
String word;
while (!(word = in.readLine()).equals("END")) {
System.out.println("Anagrams for: " + word);
int cnt = 0;
char[] str = word.toCharArray();
Arrays.sort(str);
for (int i = 0; i < N; ++i) {
char[] str2 = dict[i].toCharArray();
Arrays.sort(str2);
boolean equal = true;
if (str.length != str2.length)
equal = false;
if (equal)
for (int k = 0; k < str.length; ++k)
if (str[k] != str2[k]) {
equal = false;
break;
}
if (equal) {
++cnt;
System.out.printf("%3d) %s\n", cnt, dict[i]);
}
}
if (cnt == 0)
System.out.println("No anagrams for: " + word);
}
}
in.close();
System.exit(0);
}
}

Bài G - Educatioal Round 62

Đề bài: Bạn được cho 1 đồ thị vô hướng đặc biệt. Nó bao gồm $2n$ đỉnh được đánh số từ 1 đến 2n. Dưới đây là một số đặc tính của đồ thị: + ...