1808: 字符串的查找删除
- Time Limit: 1 Sec Memory Limit: 32 MB
- Submit: 2002 Solved: 574
- [Submit http://codeup.cn/submitpage.php?id=1808 ][Status http://codeup.cn/problemstatus.php?id=1808 ][web Board http://codeup.cn/bbs.php?pid=1808 ][Creator:Imported]
- Description
给定一个短字符串(不含空格), 再给定若干字符串, 在这些字符串中删除所含有的短字符串.
Input
输入只有 1 组数据.
输入一个短字符串(不含空格), 再输入若干字符串直到文件结束为止.
Output
删除输入的短字符串 (不区分大小写) 并去掉空格, 输出.
- Sample Input
- in
- #include
- int main()
- {
- printf("Hi");
- }
- Sample Output
- #clude
- tma()
- {
- prtf("Hi");
- }
- HINT
注: 将字符串中的 In,IN,iN,in 删除.
- #include <stdio.h>
- #include <string.h>
- int main(){
- char c[100];
- char s[1000];
- gets(c);
- int len1 = strlen(c), len2;
- // 将 c[i]全部转化为小写
- for(int i=0; i<len1; i++){
- if(c[i]>='A'&&c[i]<='Z'){
- c[i] = c[i] + 32;
- }
- }
- while(gets(s)){
- len2 = strlen(s);
- if(len2>=len1){
- // 对这个匹配过程不是很理解
- for(int i=0, k=0; i<len2; ){
- // 在匹配时, i 不变, 不匹配时, i 自增!
- if(s[i+k]==c[k] || s[i+k]==c[k]-32){
- k++;
- if(k==len1){
- i = i + k; // 在后 k 项满足时, i 增加 k
- k = 0;
- }
- }else{
- if(s[i]!=' '){
- printf("%c", s[i]);
- }
- i++;
- k = 0;
- }
- }
- }else{
- for(int i=0; i<len2; i++){
- if(s[i]!=' '){
- printf("%c", s[i]);
- }
- }
- }
- printf("\n");
- }
- return 0;
- }
来源: http://www.bubuko.com/infodetail-3365620.html