老师给小学生门布置了一些作业,让它们按照一个模版写一些字符串交上来,同学们把作业交上来了,问题来了,这么多的作业老师批改不过来,现在请你帮老师写一个程序,帮助老师确定各个字符串是否合格。首先老师有一个匹配模版,比如是“aa[123]bb”这一个字符串,同学们交的各种作业字符串如aa1bb、aa2bb、aa3bb都算是正确匹配看,而aacbb就是错误的字符串。(即待查字符串对应于模版方括号内的部分,应该为方括号内字符串的一个子字符)。我们需要做的就是按照模版,找出正确的字符串和所在的行。输入输入的第一行为一个整数n,表示有多少个学生的作业,即有多少行需要检查的字符串。(1<=n<=50) 中间为n行字符串,代表着n个学生们写的作业。每个字符串长度小于50。 最后一行为1行字符串,代表着老师给的匹配模板。输出输出合格的字符串的行号和该字符串。(中间以空格隔开)

样例输入

4

Aab

a2B

ab

ABB

a[a2b]b

样例输出

1 Aab

2 a2B

4 ABB

提示

被检测的字符串中只有数字和字母

请问这个的算法和程序是怎样的

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 作业
{
class Program
{
static void Main(string[] args)
{
int n;
n = Convert.ToInt32(Console.ReadLine());
string[] sentences = new string[n];
string[] temp = new string[n];
for (int i = ; i < sentences.Length; i++)
{
sentences[i] = Console.ReadLine();
temp[i] = sentences[i].ToUpper();
}
string template = Console.ReadLine();
template = template.ToUpper();
//至此,全部的输入完成
//首先,分析模板
int begin = template.IndexOf('[');//模板中'['的索引
int end = template.IndexOf(']');//模板中']'的索引
string keyChar = template.Substring(begin+, end - begin - );//模板中中括号里的内容
string beginStr = template.Substring(, begin);//模板中'['之前的部分
string endStr = template.Substring(end + , template.Length - end - );//模板中']'之后的部分
//模板分析完毕
for (int i = ; i < n; i++)
{
string s = temp[i];
if (s.Length == beginStr.Length + endStr.Length + )//学生的作业长度要符合标准
{
if (s.StartsWith(beginStr) || s.EndsWith(endStr))//检查学生作业的开头与结尾
{
if (keyChar.Contains(s[begin]))
{
Console.WriteLine("{0}\t{1}", i + , sentences[i]);//学生作业中的可变内容
}
}
}
} Console.ReadKey();
}
}
}

出现过的错误:经常下意识的认为索引从1开始

C#字符串题目的更多相关文章

  1. 《Cracking the Coding Interview》——第1章:数组和字符串——题目8

    2014-03-18 02:12 题目:判断一个字符串是否由另一个字符串循环移位而成. 解法:首先长度必须相等.然后将第一个串连拼两次,判断第二个串是否在这个连接串中. 代码: // 1.8 Assu ...

  2. 《Cracking the Coding Interview》——第1章:数组和字符串——题目5

    2014-03-18 01:40 题目:对字符串进行类似游程编码的压缩,如果压缩完了长度更长,则返回不压缩的结果.比如:aabcccccaaa->a2b1c5a3,abc->abc. 解法 ...

  3. 《Cracking the Coding Interview》——第1章:数组和字符串——题目4

    2014-03-18 01:36 题目:给定一个字符串,将其中的空格‘ ’替换为‘%20’,你可以认为字符串尾部有足够空间来容纳新增字符.请不要额外开辟数组完成. 解法:先从前往后统计空格个数,然后从 ...

  4. 《Cracking the Coding Interview》——第1章:数组和字符串——题目3

    2014-03-18 01:32 题目:对于两个字符串,判断它们是否是Anagrams. 解法:统计俩单词字母构成是否相同即可. 代码: // 1.3 Given two strings, write ...

  5. 《Cracking the Coding Interview》——第1章:数组和字符串——题目2

    2014-03-18 01:30 题目:反转一个char *型的C/C++字符串. 解法:一头一尾俩iterator,向中间靠拢并且交换字符. 代码: // 1.2 Implement a funct ...

  6. 《Cracking the Coding Interview》——第1章:数组和字符串——题目1

    2014-03-18 01:25 题目:给定一个字符串,判断其中是否有重复字母. 解法:对于可能有n种字符的字符集,用一个长度为n的数组统计每个字符的出现次数,大于1则表示有重复. 代码: // 1. ...

  7. 【字符串题目】poj 3096 Surprising Strings

    Surprising Strings Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6193   Accepted: 403 ...

  8. 【C++ 字符串题目】 输入三个人名,按字母顺序排序输出

    题目来源:https://acm.ujn.edu.cn Problem A: [C++ 字符串] 输入三个人名,按字母顺序排序输出 Time Limit: 1 Sec  Memory Limit: 1 ...

  9. ACM 字符串 题目整理

    AC自动机 UVa 11468  Substring AC自动机+概率DP. 注意要补全不存在的边. 为什么要补全不存在的边呢?补全以后可以直接找到状态的转移,即从所有子节点就可以实现所有状态转移. ...

随机推荐

  1. Leetcode: Range Sum Query - Mutable && Summary: Segment Tree

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  2. PHP的基本语法

    PHP的基本语法和c#的基本语法是差不多的,在这里只和大家聊一下PHP和C#语法不同的地方. 首先 PHP和c#的标记方式不一样,PHP他是一门脚本语言,JS也是脚本语言,只不过JS是运行在客户端的, ...

  3. For嵌套输出图形

    /*输出此图形    *   * *  * * * * * * ** * * * *  * * * *   * * *   * *     *解析:可以把此图形看成两部分----*---* *--* ...

  4. HDU 1890 区间反转

    http://acm.hdu.edu.cn/showproblem.php?pid=1890 Robotic Sort Problem Description Somewhere deep in th ...

  5. fzu 2188 过河I

    http://acm.fzu.edu.cn/problem.php?pid=2188 过河I Time Limit:3000MS     Memory Limit:32768KB     64bit ...

  6. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  7. struts_23_xwork校验器列表使用例子

    required 必填校验器 <field-validator type="required"> <message>性别不能为空!</message& ...

  8. 关于用php插入汉字到oracle中出现不乱码问题

    $conn = oci_pconnect("IBADMINTON", "5206576360", $db,'utf8'); 在代码中加入‘utf8’即可:   ...

  9. 04---Net基础加强

    字符串常用方法: 属性: Length获取字符串中字符的个数 IsNullOrEmpty()   静态方法,判断为null或者为“” ToCharArray() 将string转换为char[] To ...

  10. oracle的冷备份

    oracle冷备份要备份三类文件:数据文件,控制文件,日志文件 查看所有数据文件 select name from v$datafile; 查看所有日志文件 select member from v$ ...