Question T1 (application of the refining Algorithm for Array insertion and deletion)

Source: Internet
Author: User

// T1 http://acm.swust.edu.cn/oj/problem/860/.
// Analyze T1
// The linked list is useless because it is a small number of operations.
# Include <stdio. h>
# Include <string. h>
// Char str [105], cmd [6], ch [6];
// The cmd uses an array because "END" is required for receiving, and the input format of % c receives '\ n'
// The ch array is used because '\ n' is received in the input format of % c'
// 3, 6, or 1
Char str [105], cmd [3], ch [1];


Int main ()

{
Int I, j, k, l;
// Considering that the question is a multi-group Operation
While (scanf ("% s", str) = 1)
{
While (scanf ("% s", cmd) = 1 & strcmp (cmd, "END ")! = 0)
{
Scanf ("% s", ch );
L = strlen (str );
If (cmd [0] = 'A') // A operation
{
Scanf ("% d", & k );
-- K;
If (k> l) k = l;
//////////////////////////////////////// //////////////////////////////////
// This is a good piece of code with good ideas
// It is suitable for small operations, but it is not good for large operations.
// Move back to make the position of the element to be inserted
// I is the subscript of the last element.
For (I = l-1; I> = k; -- I)
Str [I + 1] = str [I];
//////////////////////////////////////// //////////////////////////////////
Str [k] = ch [0];
Str [++ l] = '\ 0'; // The end ID must be added.

} Else

{// Operation B

//////////////////////////////////////// //////////////////////////////////
// This is an excellent piece of code with excellent ideas
// It is suitable for small operations, but it is not good for large operations.
// The search, deletion, and movement of elements are well handled.
For (I = j = 0; I <l; ++ I)
{
If (str [I]! = Ch [0])
Str [j ++] = str [I];
}
Str [j] = '\ 0 ';
//////////////////////////////////////// //////////////////////////////////
}
}
Puts (str );
}
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.