B. Phoneme Palindromes
A palindrome is a string that reads the same forward and backward, e.g., madam and abba. Since some letters sound the same (e.g., c and k), we define a phoneme palindrome as a string that sounds the same forward and backward, e.g., cak and ckckbbkcck.
The Problem:
Given the letters that sound the same and a string, you are to determine if the string is a phoneme palindrome.
The Input:
The first input line contains a positive integer, n, indicating the number of test cases to process. Each test case starts with an integer, p (1 ≤ p ≤ 13), indicating the count for pairs of letters that sound the same. Each of the following p input lines provides two distinct lowercase letters (starting in column 1 and separated by a space) that sound the same. Assume that no letter appears in more than one pair. The next input line for a test case contains an integer, q (1 ≤ q ≤ 100), indicating the number of strings to test for phoneme palindrome. Each of the following qinput lines provides a string (starting in column 1 and lowercase letters only) of length 1 to 50, inclusive.
The Output:
For each test case, print the header “Test case #n:”, where n indicates the case number starting with 1. Then print each string for that test case followed by a space, followed by a message (YES or NO) indicating whether or not the string is a phoneme palindrome. Leave a blank line after the output for each test case.
解题思路是将所给的字符替换,再判断是否回文即可。
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <string> 5 #include <cstring> 6 #include <algorithm> 7 #include <iomanip> 8 #include <cstdlib> 9 #include <cctype> 10 #include <utility> 11 #define ll long long 12 #define PI 3.14159265358979323846 13 using namespace std; 14 const int maxn = 1e6+7; 15 16 char str[10000]; 17 char rstr[10000]; 18 pair<char,char> pa[100]; 19 int main(){ 20 int n; 21 cin>>n; 22 for(int m=1;m<=n;m++){ 23 if(m!=1){ 24 cout<<endl; 25 } 26 cout<<"Test case #"<<m<<":"<<endl; 27 int p; 28 cin>>p; 29 for(int i=0;i<p;i++){ 30 cin>>pa[i].first>>pa[i].second; 31 } 32 int q; 33 cin>>q; 34 for(int i=0;i<q;i++){ 35 cin>>str; 36 cout<<str; 37 for(int j=0;j<strlen(str);j++){ 38 for(int k=0;k<p;k++){ 39 if(str[j]==pa[k].first){ 40 str[j]=pa[k].second; 41 } 42 } 43 } 44 strcpy(rstr,str); 45 reverse(str,str+strlen(str)); 46 47 if(!strcmp(str,rstr)){ 48 cout<<" YES"<<endl; 49 } 50 else{ 51 cout<<" NO"<<endl; 52 } 53 // cout<<rstr<<" "<<str<<endl; 54 } 55 } 56 return 0; 57 }
Loopy Word Search
A word search puzzle is a grid of letters where your challenge is to find selected words as formed by consecutive letters in a line along the rows, columns, or diagonals of the grid. Tougher word searches also allow words in the grid to be forwards or backwards in any of those directions. In the “loopy word search”, we will also allow words to go off the edge of the grid and continue (along the same line) on the other side, and potentially even reuse letters from that same word. However, in this problem, we won't search for words along diagonal lines, i.e., we only search along the rows and columns. (The UCF programming coaches are sure nice!)
The Problem:
Given a grid of letters and a list of words, identify the location of the first letter of each word in the grid and the direction in which remaining letters of the word can be found in sequence.
The Input:
The first input line contains a positive integer, n, indicating the number of word search puzzles. This is followed by the data for these puzzles. The first input line for each puzzle contains two positive integers (separated by a space): r, the number of rows in the grid (between 3 and 12 inclusive), and c, the number of columns in the grid (between 3 and 20 inclusive). Each of the next r input lines for the puzzle contains exactly c uppercase letters, with no spaces. The next input line for each puzzle contains a positive integer s, the number of words to search for. Each of the next s input lines contains a string of uppercase letters (length between 3 and 100 letters, inclusive) which is a word to search for. It is not necessarily a real word in any language.
Each of the s words will appear exactly once in the grid, meaning it has exactly one starting location and goes only in one direction. None of the words will be palindromes (same letters backwards and forwards). Assume that the input is valid as described here.
The Output:
For each word search puzzle, output the line “Word search puzzle #p:” where p is the puzzle number (counting from 1 in the input). Then, for each word given in that puzzle (and in the order given), output a line of the form “d r c w” where w is the word, r is the row in the grid where the first letter of the word is located (counting from 1), c is the column in the grid where the first letter is located (counting from 1), and d is the direction where the remaining letters of the word can be found, relative to the first letter, as given below. Output exactly one space after each of d, r, and c. For the direction d, use the following 1-letter codes:
Leave a blank line after the output for each puzzle.
找到大写字母的开头位置,其余直接暴力搜索
这是网上上其他人的解法