Let us define GNU, the recursive acronym for GNU’s Not Unix with the following recursive rules:

  1. G –> GNU’s
  2. N –> Not
  3. U –> Unix


    In each step we apply all the rules simultaneously. If a character in a string does not have a rule associated with it (there will be at most one rule per character), it remains in the string.
For example if we start with GNU, we get:
Step String
0 GNU
1 GNU’sNotUnix
2 GNU’sNotUnix’sNototUnixnix
3 GNU’sNotUnix’sNototUnixnix’sNotototUnixnixnix
4 GNU’sNotUnix’sNototUnixnix’sNotototUnixnixnix’sNototototUnixnixnixnix
. . . . . .


    As you can see, the strings are growing larger in every steps. And in certain cases the growth can be quite fast. Fear not, for we’re not interested in the entire string. We just want to know the frequency of a particular character after a finite number of steps.
Input
The first line of the input starts with an integer, T (1 ≤ T ≤ 10), the number of test cases. Then T test cases will follow. The first line of each test case will give you R, (1 ≤ R ≤ 10) the number of rules. In each of the next R lines there will be one rule. The rules are written in the following format: ‘x->S’ (without the quotes). Here x is a single character and S is a sequence of characters. You can assume that the ASCII value of the characters lie in the range 33 to 126, that S will contain no more than 100 characters. You can also assume that the symbols ‘-’ and ‘>’ won’t appear in S. The rules can be immediately recursive, recursive in a cycle or not recursive at all. After the rules, you’ll find an integer Q (1 ≤ Q ≤ 10), the number of queries to follow. Each of the Q queries will be presented in the format “initial string x n” in a single line, where initial string is the string that you have in step 0 (with length between 1 and 100), x is the character whose frequency you should count, and n (1 ≤ n ≤ 10000) is the number of times the rules should be applied. All characters in the initial string will have ASCII values in the range 33 to 126.
Output
For each query, print the number of occurances of the particular character in the result string after applying the rules n times, starting with the initial string. The output will always fit in a 64-bit unsigned integer.
Sample Input
2
3
G->GNU’s
N->Not
U->Unix
2
GNU t 3
GNU N 3
1
A->BAcX
1
ABCcXA c 10000
Sample Output
6
4
20001

问题链接:UVA10625 GNU = GNU’sNotUnix
问题简述:(略)
问题分析
    繁琐的字符统计题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10625 GNU = GNU'sNotUnix */#include <bits/stdc++.h>using namespace std;typedef unsigned long long int ULL;const int FROM = 33, TO = 126;
const int N = 100;
char c, rule[N + 1];
int rcnt[TO + 1][TO + 1];
ULL cnt[TO + 1], tmp[TO + 1];int main()
{int t, r, q, n;scanf("%d", &t);while(t--) {scanf("%d", &r);getchar();memset(rcnt, 0, sizeof(rcnt));while(r--) {scanf("%c->%s", &c, rule);getchar();for(int i = 0; rule[i]; i++)rcnt[(int)c][(int)rule[i]]++;}scanf("%d", &q);while(q--) {memset(cnt, 0, sizeof(cnt));memset(tmp, 0, sizeof(tmp));scanf("%s %c %d", rule, &c, &n);for(int i = 0; rule[i]; i++)cnt[(int)rule[i]]++;while(n--) {for(int i = FROM; i <= TO; i++) {ULL times = cnt[i];bool flag = false;for(int j = FROM; j <= TO; j ++) {if(rcnt[i][j] > 0) {tmp[j] += times * rcnt[i][j];flag = true;}}if(!flag) tmp[i] += times;      // 考虑字母未转换的情况}memcpy(cnt, tmp, sizeof(cnt));memset(tmp, 0, sizeof(tmp));}printf("%llu\n", cnt[(int)c]);}}return 0;
}

UVA10625 GNU = GNU'sNotUnix【字符统计】相关推荐

  1. PAT 1042. 字符统计

    1042. 字符统计 请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过1000的字符串.字符串由ASCII码表中任意可见字符及空格组成,至少包含1个 ...

  2. idf实验室--简单编程字符统计

    idf实验室--简单编程字符统计,有需要的朋友可以参考下. 第一眼看这道题很简单,不就是字符统计么,可是题目要求2s内回答,而且每次打开的页面需要统计的字符串内容都会变,这就蛋疼了,于是乎上网学习下如 ...

  3. C++学习之路 | PTA乙级—— 1042 字符统计 (20 分)(精简)

    1042 字符统计 (20 分) 请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过 1000 的字符串.字符串由 ASCII 码表中任意可见字符及空 ...

  4. 去空白符的大文本字符统计(洛谷P5015题题解,Java语言描述)

    题目要求 P5015题目链接 分析 字符串中的字符统计,不含换行符和空白字符,那就正好用Scanner的nextLine(),就自动去了换行符. 接下来每读进来一行String.就调用replaceA ...

  5. 今天用Map集合写了一个字符串字符统计的程序,看集合看的头痛,就看了一下GUI,于是就随便记点。

    一.使用Map集合写的字符串中字符统计功能函数. package Map;import java.util.HashMap; import java.util.Iterator; import jav ...

  6. 【小白的刷题之路】字符统计

    字符统计 题目描述 代码 题目描述 如果统计的个数相同,则按照ASCII码由小到大排序输出 .如果有其他字符,则对这些字符不用进行统计. 实现以下接口: 输入一个字符串,对字符中的各个英文字符,数字, ...

  7. Bailian2927 判断数字个数【字符统计】

    2927:判断数字个数 总时间限制: 1000ms 内存限制: 65536kB 描述 判断一行字符串中的数字出现的个数. 输入 输入有多行,每行一个字符串,该字符串中可以包含&$#@任何符号以 ...

  8. 汇编语言程序设计实验——字符统计

    [实验题目]字符统计 [实验内容]从键盘上输入一个字符串,然后统计英文字母.数字字符和其他字符的个数并输出 [实验目的] 1. 掌握输入字符串的DOS功能调用 2. 掌握分支程序设计方法 3. 掌握二 ...

  9. XDOJ指针 字符统计

    XDOJ - 字符统计 描述: 定义一个一维字符数组string[100],输入一个字符串,含N个字符(N≤100),定义一个整形数组num[5],用于存放统计结果数据,编写函数count()统计字符 ...

最新文章

  1. Ubuntu16.04运行.run文件
  2. linux shell脚本if,linux的shell脚本中if,for,while的解析与应用
  3. 【查询】—Entity Framework实例详解
  4. 光缆连接头符号缩写详解
  5. 159. Leetcode 122. 买卖股票的最佳时机 II (贪心算法-股票题目)
  6. ML之DT(树模型):DT(树模型算法)算法的简介、代码定义、案例应用之详细攻略
  7. 算法 | 数据结构与算法(代码版)
  8. 迄今为止用到的Eclipse快捷键,最常用的--[欢迎补充]
  9. go float64 转int_深挖Go函数之深度解析(下):可变参数
  10. 基本shell编程【3】- 常用的工具awk\sed\sort\uniq\od
  11. 怎样更改SQL Server 2008的身份验证方式
  12. 数据库还原的多种方式
  13. spark sample采样
  14. Vue弹性标题栏(收缩扩张标题栏背景)
  15. 省钱兄霸王餐源码uniapp前端
  16. 微信应用架构!!-----微信应用能力篇
  17. 如何在MacBook Pro上使用原彩显示功能?
  18. Bresenham快速画直线算法
  19. CSDN图片上传去除水印
  20. 【路径规划】样条曲线

热门文章

  1. 深入浅出Docker(六):像谷歌一样部署你的应用
  2. OGRE 1.7.2 [Cthugha] 编译方法
  3. tensorflow 显存 训练_tensorflow手动指定GPU以及显存大小
  4. html让时间只展示年月日_JS 如何动态显示当前年月日时分秒-百度经验
  5. java 显示锁_Java 实现一个自己的显式锁Lock(有超时功能)
  6. Android Studio导入model
  7. 大数据-概念-应用-弊端
  8. Spark-生产案例
  9. java数据类型单位_java的数据类型
  10. IntelliJ IDEA 激活