ZCMU—1798
来源:互联网 发布:数控车床编程实例 编辑:程序博客网 时间:2024/06/05 09:06
Problem B: Problem B: Language Cardinality
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 6 Solved: 2
[Submit][Status][Web Board]
Description
Problem B: Language Cardinality
A (formal) language is a set of strings. One way to define a particular langauge is using ordinary set notation. Alternatively, some form of grammar may be more convenient for representing large sets. The UW grammar in which we are interested has two parts:
- An initial string
- A set of replacement rules of the form s1 -> s2 where s1 and s2 are strings
The language defined by this grammar is the set of all strings that can be generated by repeatedly replacing s1 by s2 within the initial string. For example, consider the grammar G consisting of the initial string
"AyB"
and the replacement rules
{"A"->"ab", "Ay"->"cdy", "B"->"w", "B"->"x"} .
G generates the language
L = {"AyB", "Ayw", "Ayx", "abyB", "abyw", "abyx", "cdyB", "cdyw", "cdyx"}
Given a UW grammar G, compute how many different strings there are in the language generated by G.
The first line of input contains the initial string. The second and subsequent lines contain the replacement rules, one per line, terminated by end-of-file. There are at most 100 replacement rules. Each input string contains between 0 and 10 upper and lower case letters, and is enclosed in quotes. There are no spaces in the input.
Output consists of a single integer, the number of distinct strings in the language generated by G. If there are more than 1000 distinct strings, print "Too many." instead.
Input
Output
Sample Input
Sample Output
【分析】
#include <iostream>#include <set>#include <cstdio>#include <cstring>#include <vector>using namespace std;string x,s;set <string> a;vector <string> f;string ma[10000][3];int len,i,t,tt;int main(){cin>>x;x.erase(x.begin());x.erase(x.end()-1);a.insert(x);f.push_back(x);len=0;int flag=0;while (cin>>x){for (i=1;!(x[i+1]=='-'&& x[i+2]=='>');i++) ma[len][0]+=x[i];for (i+=4;i<x.length()-1;i++) ma[len][1]+=x[i];if (ma[len][1].find(ma[len][0])!=string::npos&& ma[len][1]!=ma[len][0]) flag=1;len++;}while (!flag){x=f.front();f.erase(f.begin());for (i=0;i<len;i++){t=x.find(ma[i][0]);while (t!=string::npos&&a.size()<=1000){s=x;s.replace(t,ma[i][0].length(),ma[i][1]);tt=a.size();a.insert(s);if (tt<a.size()) f.push_back(s);t=x.find(ma[i][0],t+1);}}if (f.empty()||a.size()>1000) break;}if (a.size()>1000||flag) printf("Too many.\n");else printf("%d\n",a.size());}
- ZCMU—1798
- ZCMU—1678
- ZCMU—1605
- ZCMU—1067
- ZCMU—1607
- ZCMU—1069
- ZCMU—1611
- ZCMU—1606
- ZCMU—1774
- ZCMU—1766
- ZCMU—1752
- ZCMU—1724
- ZCMU—1623
- ZCMU—1689
- ZCMU—1667
- ZCMU—1639
- ZCMU—1668
- ZCMU—1540
- 相对定位和绝对定位的区别
- JAVA 通过poi 读取 本地excel(.xls,.xlsx)文件,文件中 包含图片
- Oracle卸载
- Android之旅第一站——第一行代码…
- Java微信公众平台开发之OAuth2.0网页授权
- ZCMU—1798
- 布局
- 把第一次写的题解转到这里来了
- unity3d开发 打飞机小游戏(三)(飞机子弹设置)
- linux中网络配置与修改
- @weak - @strong 宏的实现
- 坚持到底十步学习
- struts2.3.31+spring4.3.3+hibernate5.1.3集成
- poi常用设置