6字符串处理.ppt





《6字符串处理.ppt》由会员分享,可在线阅读,更多相关《6字符串处理.ppt(33页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、第六讲第六讲字符串字符串处处理理ACMACM算法与程序设计算法与程序设计Look and Say 1、链接地址、链接地址http:/ look and say sequence is defined as follows.Start with any string of digits as the first element in the sequence.Each subsequent element is defined from the previous one by verbally describing the previous element.For example,the str
2、ing 122344111 can be described as one 1,two 2s,one 3,two 4s,three 1s.Therefore,the element that comes after 122344111 in the sequence is 1122132431.Similarly,the string 101 comes after 1111111111.2InputThe input consists of a number of cases.The first line gives the number of cases to follow.Each ca
3、se consists of a line of up to 1000 digits.OutputFor each test case,print the string that follows the given string.Sample Input3122344111111111111112345Sample Output1122132431101111213141533、解题思路解题思路本题是处理重复子串的问题。虽然输入的都是数字,本题是处理重复子串的问题。虽然输入的都是数字,但应当把它们当成字符串处理。但应当把它们当成字符串处理。由于本题时限一秒,每个字符串的长度多达由于本题时限一秒
4、,每个字符串的长度多达1000位,位,所以,不好的算法容易超时。所以,不好的算法容易超时。scanf和和printf所用的时间大大少于所用的时间大大少于cin和和cout所消耗的时所消耗的时间。由于本题需要频繁输出,采用间。由于本题需要频繁输出,采用cout则会超过一秒;则会超过一秒;但使用但使用printf则不会超过一秒。这点是则不会超过一秒。这点是ACM竞赛中节约竞赛中节约时间的常识。一般地,由于时间的常识。一般地,由于cin和和cout调试很方便,所调试很方便,所以调试期间使用它们,但是提交判题系统后,如果发以调试期间使用它们,但是提交判题系统后,如果发现超时,可尝试将现超时,可尝试将c
5、in和和cout改为改为scanf和和printf,看看是,看看是不是由于输入输出过于频繁而导致的。不是由于输入输出过于频繁而导致的。44、参考程序、参考程序#include#includeint main()char s1001,t;int c,i,j,n,len,temp;scanf(%d,&n);for(i=0;in;i+)scanf(%s,s);c=0;t=s0;temp=0;len=strlen(s);54、参考程序、参考程序for(j=0;jlen;j+)if(sj=t)temp+;if(j=len-1)printf(%d%c,temp,t);elseprintf(%d%c,tem
6、p,t);t=sj;temp=1;if(j=len-1)printf(%d%c,temp,t);printf(n);return 0;6Abbreviation1、链接地址、链接地址http:/ a Little White meets another Little White:Little White A:(Surprised)!Little White B:?Little White A:You Little White know SHDC?So unbelievable!Little White B:You are little white!Little white is you!Wha
7、t is SHDC you are talking about?Little White A:Wait.I mean Super Hard-disc Drive Cooler.Little White B:I mean Spade Heart Diamond Club.Duck talks with chicken-_-/-_-/Little White A:Duck.chicken.faint!7-quote from qmd of Spade6 in CC98 forum.Sometimes,we write the abbreviation of a name.For example I
8、BM is the abbreviation for International Business Machines.A name usually consists of one or more words.A word begins with a capital letter(A-Z)and followed by zero or more lower-case letters(a-z).The abbreviation for a name is the word that consists of all the first letters of the words.Now,you are
9、 given two names and asked to decide whether their abbreviations are the same.8InputStandard input will contain multiple test cases.The first line of the input is a single integer T which is the number of test cases.And it will be followed by T consecutive test cases.There are four lines for each ca
10、se.The first line contains an integer N(1=N=5),indicating the number of words in the first name.The second line shows the first name.The third line contains an integer M(1=M=5),indicating the number of words in the second name.The fourth line shows the second name.Each name consists of several words
11、 separated by space.Length for every word is less than 10.The first letter for each word is always capital and the rest ones are lower-case.9OutputResults should be directed to standard output.The output of each test case should be a single line.If two names abbreviations are the same,output SAME,ot
12、herwise output DIFFERENT.10Sample Input34Super Harddisc Drive Cooler4Spade Heart Diamond Club3Shen Guang Hao3Shuai Ge Hao3Cai Piao Ge4C P C SSample OutputSAMESAMEDIFFERENT113、解题思路解题思路 本题是比较两个缩写词是否相同,而缩写词又是从一本题是比较两个缩写词是否相同,而缩写词又是从一个包含多个单词的名字中合成的。每次读入一个单词,个包含多个单词的名字中合成的。每次读入一个单词,然后取出它的第一个单词,连接在字符串上,就组
13、成然后取出它的第一个单词,连接在字符串上,就组成一个缩写词。一个缩写词。本题的难点在单词的读取控制上,对于输入数据的控本题的难点在单词的读取控制上,对于输入数据的控制,是制,是ACM竞赛中考查的一个重要方面。竞赛中考查的一个重要方面。另外,大家可以试试,使用另外,大家可以试试,使用printf输出比使用输出比使用cout输出输出快很多。快很多。124、参考程序、参考程序#include#includeint main()char s11,ssa6,ssb6;int i,j,t,n;scanf(%d,&t);for(i=0;it;i+)scanf(%d,&n);for(j=0;jn;j+)sca
14、nf(%s,s);ssaj=s0;/只取首字母只取首字母ssaj=0;/作字符串处理作字符串处理scanf(%d,&n);for(j=0;jn;j+)scanf(%s,s);ssbj=s0;ssbj=0;if(strcmp(ssa,ssb)=0)printf(SAMEn);else printf(DIFFERENTn);return 0;13一个简单的字符串操作的例子一个简单的字符串操作的例子n n#include#includen n#include#includen nchar char strlstrl=“The quick brown dog jumps over the lazy f
15、ox”;=“The quick brown dog jumps over the lazy fox”;n nchar str250=“The QUICK brown dog Jumps over the lazy fox”char str250=“The QUICK brown dog Jumps over the lazy fox”;n nchar str340=“The QUICK brown dog Jumps over the lazy fox”char str340=“The QUICK brown dog Jumps over the lazy fox”;n n/错误:字符串共有错
16、误:字符串共有错误:字符串共有错误:字符串共有4343个字符,需要一个长度至少为个字符,需要一个长度至少为个字符,需要一个长度至少为个字符,需要一个长度至少为4444的字符串变量存储。的字符串变量存储。的字符串变量存储。的字符串变量存储。n n/易忽略在字符串的末尾要添加表示结束的额外标志字符易忽略在字符串的末尾要添加表示结束的额外标志字符易忽略在字符串的末尾要添加表示结束的额外标志字符易忽略在字符串的末尾要添加表示结束的额外标志字符/0/0。n nchar str450char str450;n nvoid void main(voidmain(void)n n n n intint res
17、ult;result;n n str4=“The QUICK brown DOG jumps over the lazy fox”;str4=“The QUICK brown DOG jumps over the lazy fox”;n n /错误:不能将一个字符串常量赋值给另一个字符串变量。错误:不能将一个字符串常量赋值给另一个字符串变量。错误:不能将一个字符串常量赋值给另一个字符串变量。错误:不能将一个字符串常量赋值给另一个字符串变量。n n str4=str2;/str4=str2;/错误:不能将一个字符串变量赋值绘另一个字符串变量错误:不能将一个字符串变量赋值绘另一个字符串变量错误:不
18、能将一个字符串变量赋值绘另一个字符串变量错误:不能将一个字符串变量赋值绘另一个字符串变量n n str4=str1;/str4=str1;/错误:不能将一个字符串变量赋值给另一个字符串变量错误:不能将一个字符串变量赋值给另一个字符串变量错误:不能将一个字符串变量赋值给另一个字符串变量错误:不能将一个字符串变量赋值给另一个字符串变量n n printf(“Compareprintf(“Compare strings:strings:nt%snt%snn”,strlnt%snt%snn”,strl,str2);str2);n n 14487-3279 http:/ Central North Am
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 字符串 处理

限制150内