UVa-272.
Description:

TEX is a typesetting language developed by Donald Knuth. It takes source text together with a few

typesetting instructions and produces, one hopes, a beautiful document. Beautiful documents use “

and ” to delimit quotations, rather than the mundane " which is what is provided by most keyboards.

Keyboards typically do not have an oriented double-quote, but they do have a left-single-quote and a right-single-quote '. Check your keyboard now to locate the left-single-quote key (sometimes

called the “backquote key”) and the right-single-quote key ' (sometimes called the “apostrophe” or

just “quote”). Be careful not to confuse the left-single-quote ` with the “backslash” key. TEX lets

the user type two left-single-quotes to create a left-double-quote “ and two right-single-quotes '' to create a right-double-quote ”. Most typists, however, are accustomed to delimiting their quotations with the un-oriented double-quote ".If the source contained "To be or not to be," quoth the bard, "that is the question." then the typeset document produced by TEX would not contain the desired form: “To be or not to be,” quoth the bard, “that is the question.” In order to produce the desired form, the source file must contain the sequence:To be or not to be,'' quoth the bard, that is the question.'' You are to write a program which converts text containing double-quote (") characters into text that is identical except that double-quotes have been replaced by the two-character sequences required by TEX for delimiting quotations with oriented double-quotes. The double-quote (") characters should be replaced appropriately by either if the " opens a quotation and by '' if the " closes a quotation.

Notice that the question of nested quotations does not arise: The first " must be replaced by , the next by '', the next by , the next by '', the next by ``, the next by '', and so on.

Input:

Input will consist of several lines of text containing an even number of double-quote (") characters.

Input is ended with an end-of-file character.

Output:

The text must be output exactly as it was input except that:

• the first " in each pair is replaced by two ` characters: `` and

• the second " in each pair is replaced by two ' characters: ''.

Sample Input:

"To be or not to be," quoth the Bard, "that

is the question".

The programming contestant replied: "I must disagree.

To C' or not to C', that is The Question!"

Sample Output:

To be or not to be,'' quoth the Bard, that

is the question''.

The programming contestant replied: ``I must disagree.

To C' or not to C', that is The Question!''

Codes:
//#define LOCAL

#include <cstdio>

int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int c, q = 1;
while((c=getchar()) != EOF) {
if(c == '"') {
printf("%s", q?"``":"''");
q = !q;
} else
printf("%c", c);
} return 0;
}
UVa-10082.
Description:

A common typing error is to place the hands on the keyboard one row to the right of the correct position. So ‘Q’ is typed as ‘W’ and ‘J’ is typed as ‘K’ and

so on. You are to decode a message typed in this manner.

Input:

Input consists of several lines of text. Each line may contain digits, spaces, upper case letters (except

Q, A, Z), or punctuation shown above [except back-quote (‘)]. Keys labelled with words [Tab, BackSp,

Control, etc.] are not represented in the input.

Output:

You are to replace each letter or punction symbol by the one immediately to its left on the ‘QWERTY’

keyboard shown above. Spaces in the input should be echoed in the output.

Sample Input:

O S, GOMR YPFSU/

Sample Output:

I AM FINE TODAY.

Codes:
//#define LOCAL

#include <cstdio>

char s[] = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";

int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int i, c;
while((c=getchar()) != EOF) {
for(i=1; s[i]&&s[i]!=c; ++i);
if(s[i]) putchar(s[i-1]);
else putchar(c);
} return 0;
}
UVa-401.
Description:

A regular palindrome is a string of numbers or letters that is the same forward as backward. For

example, the string “ABCDEDCBA” is a palindrome because it is the same when the string is read from

left to right as when the string is read from right to left.

A mirrored string is a string for which when each of the elements of the string is changed to its

reverse (if it has a reverse) and the string is read backwards the result is the same as the original string.

For example, the string “3AIAE” is a mirrored string because ‘A’ and ‘I’ are their own reverses, and ‘3’

and ‘E’ are each others’ reverses.

A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria

of a mirrored string. The string “ATOYOTA” is a mirrored palindrome because if the string is read

backwards, the string is the same as the original and because if each of the characters is replaced by

its reverse and the result is read backwards, the result is the same as the original string. Of course, ‘A’,

‘T’, ‘O’, and ‘Y’ are all their own reverses.

A list of all valid characters and their reverses is as follows.

Character Reverse Character Reverse Character Reverse

A A M M Y Y

B N Z 5

C O O 1 1

D P 2 S

E 3 Q 3 E

F R 4

G S 2 5 Z

H H T T 6

I I U U 7

J L V V 8 8

K W W 9

L J X X

Note that ‘0’ (zero) and ‘O’ (the letter) are considered the same character and therefore ONLY the

letter ‘O’ is a valid character.

Input:

Input consists of strings (one per line) each of which will consist of one to twenty valid characters.

There will be no invalid characters in any of the strings. Your program should read to the end of file.

Output:

For each input string, you should print the string starting in column 1 immediately followed by exactly

one of the following strings.

STRING CRITERIA

‘ -- is not a palindrome.’ if the string is not a palindrome and is not a mirrored string

‘ -- is a regular palindrome.’ if the string is a palindrome and is not a mirrored string

‘ -- is a mirrored string.’ if the string is not a palindrome and is a mirrored string

‘ -- is a mirrored palindrome.’ if the string is a palindrome and is a mirrored string

Note that the output line is to include the ‘-’s and spacing exactly as shown in the table above and

demonstrated in the Sample Output below.

In addition, after each output line, you must print an empty line.

Sample Input:

NOTAPALINDROME

ISAPALINILAPASI

2A3MEAS

ATOYOTA

Sample Output:

NOTAPALINDROME -- is not a palindrome.

ISAPALINILAPASI -- is a regular palindrome.

2A3MEAS -- is a mirrored string.

ATOYOTA -- is a mirrored palindrome.

Codes:
//#define LOCAL

#include <cstdio>
#include <cstring>
#include <cctype> const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";
const char* msg[] = {
"not a palindrome",
"a regular palindrome",
"a mirrored string",
"a mirrored palindrome"
}; char r(char ch) {
if(isalpha(ch)) return rev[ch-'A'];
return rev[ch-'0'+25];
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif char s[30];
while(scanf("%s", s) == 1) {
int len = strlen(s);
int p = 1, m = 1;
for(int i=0; i<(len+1)/2; ++i) {
if(s[i] != s[len-1-i]) p = 0;
if(r(s[i]) != s[len-1-i]) m = 0;
}
printf("%s -- is %s.\n\n", s, msg[m*2+p]);
} return 0;
}
UVa-340.
Description:

MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker,

tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players

agree upon the length N that a code must have and upon the colors that may occur in a code.

In order to break the code, Breaker makes a number of guesses, each guess itself being a code. After

each guess Designer gives a hint, stating to what extent the guess matches his secret code.

In this problem you will be given a secret code s 1 ...s n and a guess g 1 ...g n , and are to determine

the hint. A hint consists of a pair of numbers determined as follows.

A match is a pair (i,j), 1 ≤ i ≤ n and 1 ≤ j ≤ n, such that s i = g j . Match (i,j) is called strong

when i = j, and is called weak otherwise. Two matches (i,j) and (p,q) are called independent when

i = p if and only if j = q. A set of matches is called independent when all of its members are pairwise

independent.

Designer chooses an independent set M of matches for which the total number of matches and the

number of strong matches are both maximal. The hint then consists of the number of strong followed

by the number of weak matches in M. Note that these numbers are uniquely determined by the secret

code and the guess. If the hint turns out to be (n,0), then the guess is identical to the secret code.

Input:

The input will consist of data for a number of games. The input for each game begins with an integer

specifying N (the length of the code). Following these will be the secret code, represented as N integers,

which we will limit to the range 1 to 9. There will then follow an arbitrary number of guesses, each

also represented as N integers, each in the range 1 to 9. Following the last guess in each game will be

N zeroes; these zeroes are not to be considered as a guess.

Following the data for the first game will appear data for the second game (if any) beginning with a

new value for N. The last game in the input will be followed by a single ‘0’ (when a value for N would

normally be specified). The maximum value for N will be 1000.

Output:

The output for each game should list the hints that would be generated for each guess, in order, one hint

per line. Each hint should be represented as a pair of integers enclosed in parentheses and separated by

a comma. The entire list of hints for each game should be prefixed by a heading indicating the game

number; games are numbered sequentially starting with 1. Look at the samples below for the exact

format.

Sample Input:

4

1 3 5 5

1 1 2 3

4 3 3 5

6 5 5 1

6 1 3 5

1 3 5 5

0 0 0 0

10

1 2 2 2 4 5 6 6 6 9

1 2 3 4 5 6 7 8 9 1

1 1 2 2 3 3 4 4 5 5

1 2 1 3 1 5 1 6 1 9

1 2 2 5 5 5 6 6 6 7

0 0 0 0 0 0 0 0 0 0

0

Sample Output:

Game 1:

(1,1)

(2,0)

(1,2)

(1,2)

(4,0)

Game 2:

(2,4)

(3,2)

(5,0)

(7,0)

Codes:
//#define LOCAL

#include <cstdio>

#define maxn 1010

int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int n, a[maxn], b[maxn];
int kase = 0; while(scanf("%d", &n)==1 && n) {
printf("Game %d:\n", ++kase);
for(int i=0; i<n; ++i) scanf("%d", &a[i]);
for(;;) {
int A = 0, B = 0;
for(int i=0; i<n; ++i) {
scanf("%d", &b[i]);
if(a[i] == b[i]) ++A;
}
if(!b[0]) break;
for(int d=1; d<=9; ++d) {
int c1 = 0, c2 = 0;
for(int i=0; i<n; ++i) {
if(a[i] == d) ++c1;
if(b[i] == d) ++c2;
}
if(c1 < c2) B += c1;
else B += c2;
}
printf(" (%d,%d)\n", A, B-A);
}
} return 0;
}
UVa-1583.
Description:

For a positive integer N, the digit-sum of N is defined as the sum of N itself and its digits. When M

is the digitsum of N, we call N a generator of M.

For example, the digit-sum of 245 is 256 (= 245 + 2 + 4 + 5). Therefore, 245 is a generator of

256.

Not surprisingly, some numbers do not have any generators and some numbers have more than one

generator. For example, the generators of 216 are 198 and 207.

You are to write a program to find the smallest generator of the given integer.

Input:

Your program is to read from standard input. The input consists of T test cases. The number of test

cases T is given in the first line of the input. Each test case takes one line containing an integer N,

1 ≤ N ≤ 100,000.

Output:

Your program is to write to standard output. Print exactly one line for each test case. The line is to

contain a generator of N for each test case. If N has multiple generators, print the smallest. If N does

not have any generators, print ‘0’.

Sample Input:

3

216

121

2005

Sample Output:

198

0

1979

Codes:
//#define LOCAL

#include <cstdio>
#include <cstring> #define maxn 100005
int ans[maxn]; int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int T, n;
memset(ans, 0, sizeof(ans));
for(int m=1; m<maxn; ++m) {
int x = m, y = m;
while(x > 0) {
y += x%10;
x /= 10;
}
if(ans[y]==0 || m<ans[y]) ans[y] = m;
} scanf("%d", &T);
while(T--) {
scanf("%d", &n);
printf("%d\n", ans[n]);
} return 0;
}
UVa-1584.
Description:

Some DNA sequences exist in circular forms as in

the following figure, which shows a circular sequence

“CGAGTCAGCT”, that is, the last symbol “T” in

“CGAGTCAGCT” is connected to the first symbol “C”. We al-

ways read a circular sequence in the clockwise direction.

Since it is not easy to store a circular sequence in a com-

puter as it is, we decided to store it as a linear sequence.

However, there can be many linear sequences that are ob-

tained from a circular sequence by cutting any place of the

circular sequence. Hence, we also decided to store the linear

sequence that is lexicographically smallest among all linear

sequences that can be obtained from a circular sequence.

Your task is to find the lexicographically smallest sequence

from a given circular sequence. For the example in the figure,

the lexicographically smallest sequence is “AGCTCGAGTC”. If there are two or more linear sequences that

are lexicographically smallest, you are to find any one of them (in fact, they are the same).

Input:

The input consists of T test cases. The number of test cases T is given on the first line of the input

file. Each test case takes one line containing a circular sequence that is written as an arbitrary linear

sequence. Since the circular sequences are DNA sequences, only four symbols, ‘A’, ‘C’, ‘G’ and ‘T’, are

allowed. Each sequence has length at least 2 and at most 100.

Output:

Print exactly one line for each test case. The line is to contain the lexicographically smallest sequence

for the test case.

Sample Input:

2

CGAGTCAGCT

CTCC

Sample Output:

AGCTCGAGTC

CCCT

Codes:
//#define LOCAL

#include <cstdio>
#include <cstring> #define maxn 105
char s[maxn]; int less(const char* s, int p, int q) {
int n = strlen(s);
for(int i=0; i<n; ++i)
if(s[(p+i)%n] != s[(q+i)%n])
return s[(p+i)%n] < s[(q+i)%n];
return 0;
} int main()
{
#ifdef LOCAL
freopen("E:\\Temp\\input.txt", "r", stdin);
freopen("E:\\Temp\\output.txt", "w", stdout);
#endif int T;
scanf("%d", &T); while(T--) {
scanf("%s", s);
int ans = 0;
int n = strlen(s);
for(int i=1; i<n; ++i)
if(less(s, i, ans)) ans = i;
for(int i=0; i<n; ++i)
putchar(s[(i+ans)%n]);
putchar('\n');
} return 0;
}

UVa/数组和字符串习题集的更多相关文章

  1. UVa/数组与字符串习题集

    UVa-272. Description: TEX is a typesetting language developed by Donald Knuth. It takes source text ...

  2. Base-64 字符数组或字符串的长度无效等问题解决方案

    项目特殊需要,调用ActiveX三维控件进行控件某一特殊部位的截图操作,这个截图保存由ActiveX控件控制保存到本地是没问题的,现在需要将这个截图上传到服务器,多人共享,就牵扯到需要读取本地文件…… ...

  3. c数组与字符串

    原文链接:http://www.orlion.ga/913/ 一.数组 定义数组: int count[9]; 赋值: int count[4] = { 3, 2, }; 未赋初值的元素用0初始化.如 ...

  4. C语言 第七章 数组与字符串

    一.数组 1.1.数组的概念 用来存储一组相同类型数据的数据结构.有点像班上放手机的手机袋,超市的储物柜. 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. ...

  5. js对象、数组转换字符串

    对象转换成字符串需要使用toString()方法. 1 var a = function(){ 2 console.log(111); 3 }; 4 var b = a.toString(); 5 c ...

  6. javascript中数组和字符串的方法比较

    × 目录 [1]可索引 [2]转换 [3]拼接[4]创建[5]位置 前面的话 字符串和数组有很多的相同之处,它们的方法众多,且相似度很高:但它们又有不同之处,字符串是不可变值,于是可以把其看作只读的数 ...

  7. json对象,数组,字符串总结

    关于json对象,数组,字符串的总结 什么是json? JSON(JavaScript Object Notation)  一种轻量级的数据交换格式,JSON采用完全独立于语言的文本格式...(来自百 ...

  8. @proprety数组字典字符串用copy和strong区别(深浅拷贝)

    ////  @proprety数组字典字符串用copy和strong区别(深浅拷贝).h//  IOS笔记//// /* _proprety________copy_strong_________h ...

  9. php byte数组与字符串转换类

    <?php /** * byte数组与字符串转化类 * @author ZT */ class Bytes { /** * 转换一个string字符串为byte数组 * @param $str ...

随机推荐

  1. 微软.NET年芳15:我在Azure上搭建Photon服务器(C#.NET)

    网上火热的“微软.NET年芳15”文章,我也得写点什么嘛,毕竟我还是现任的微软MVP. 摘录网上的“.NET 15周年”信息如下: 微软的 .NET 框架本周迎来了 15 岁生日..NET 的第一个版 ...

  2. Python入门教程(1)

    人生苦短,我用Python! Python(英语发音:/ˈpaɪθən/), 是一种面向对象.解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于19 ...

  3. TableView 多余分割线的处理

    方法一,以下两个方法的实现 - (void)viewDidLoad { [super viewDidLoad]; self.tableView.tableFooterView = [[UIView a ...

  4. .NET 知识整理笔记

    本文纯属自己理解着写的,如果有什么错误或者不全面希望读者能够加以补充!~ 1.Web.Config配置文件的节点操作. 在Web.Config中写入. <configuration> &l ...

  5. PHP命名空间的概念与使用

    命名空间在其它编程语言中其名称不尽相同,但其核心慨念都是自定义一个存储空间.避免类名重复系统无法判断该执行哪一个类或是哪一个函数. 举例说明下.我先创建test这个文件夹在其当前目录下再创建一个ind ...

  6. tp框架之对列表的一系列操作及跳转页面(详细步骤)

    依旧是在Main控制器里面写类方法,如果想看tp全部的话,可以从前几篇开始看,都是一整个步骤下来的 在控制器中重新写个类 然后再做个shouye.html页面 nation表的数据,将会在shou.h ...

  7. Java 集合的理解(持续更新......)

    一.集合的由来 通常,我们的程序需要根据程序运行时才知道创建多少个对象.但若非程序运行,程序开发阶段,我们根本不知道到底需要多少个数量的对象,甚至不知道它的准确类型.为了满足这些常规的编程需要,我们要 ...

  8. 3-14 JS基础知识01

    JavaScript的组成: JS特点:JS是一门 脚本语言:不需要编译编译:把代码转化成计算机所认知的二进制语言.JS是一门弱类型语言:声明变量都用varJS是一种动态语言:认知当前的着这个变量到底 ...

  9. 10分钟精通SharePoint - SharePoint升级

    类型: b2b(安装更新)和v2v(跨版本升级) 内容:二进制文件和数据库 过程:   升级前检查 - 检查场内数据,配置和自定义等等 升级准备和计划 - 根据需要和升级检查制定相应计划和准备工作   ...

  10. javascript继承--原型链的 继承

    作者的话:原型链是JavaScript中相当重要的一个知识点,这里我使用了函数结构图,来帮助我更好的理解 /* 原型链继承方式: 通过改变一个对象的原型对象的指向来继承另一个对象 原理: 我们知道,一 ...