天天看點

zoj 1205 + zoj 1016(模拟)

Martian Addition(模拟)

Time Limit: 2 Seconds      Memory Limit: 65536 KB

  In the 22nd Century, scientists have discovered intelligent residents live on the Mars. Martians are very fond of mathematics. Every year, they would hold an Arithmetic Contest on Mars (ACM). The task of the contest is to calculate the sum of two 100-digit numbers, and the winner is the one who uses least time. This year they also invite people on Earth to join the contest.

  As the only delegate of Earth, you're sent to Mars to demonstrate the power of mankind. Fortunately you have taken your laptop computer with you which can help you do the job quickly. Now the remaining problem is only to write a short program to calculate the sum of 2 given numbers. However, before you begin to program, you remember that the Martians use a 20-based number system as they usually have 20 fingers.

Input:

You're given several pairs of Martian numbers, each number on a line.

Martian number consists of digits from 0 to 9, and lower case letters from a to j (lower case letters starting from a to present 10, 11, ..., 19).

The length of the given number is never greater than 100.

Output:

For each pair of numbers, write the sum of the 2 numbers in a single line.

Sample Input:

1234567890
abcdefghij
99999jjjjj
9999900001
           
Sample Output:
bdfi02467j
iiiij00000
           

【分析】就是二十進制運算....我也不知道為什麼 寫了一個下午...不知道最近什麼情況,寫什麼都要寫很久很久....

注意特例,01+0000000000001;0+0;就是前導很多0的時候要處理一下。然後就是,長度不一樣的話,前補0;

其他好像沒什麼要注意的。反正就是菜得不行,找錯誤找了那麼久。。好煩。。。

【代碼】

#include<bits/stdc++.h>
using namespace std;

const int maxn=110;
int a[maxn],b[maxn];

string add(string x,string y)
{
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    int len1=x.length(),len2=y.length();
    for(int i=0;i<len1;++i)a[len1-1-i]=(isdigit(x[i]))?x[i]-'0':x[i]-'a'+10;
    for(int i=0;i<len2;++i)b[len2-1-i]=(isdigit(y[i]))?y[i]-'0':y[i]-'a'+10;
    int len=max(len1,len2);
    string ans="";
    for(int i=0;i<len;++i)
    {
        a[i]+=b[i];
        a[i+1]+=a[i]/20;
        a[i]%=20;
    }
    if(a[len])len++;
    for(int i=len-1;i>=0;--i)
    {
        if(a[i]<10)ans+=a[i]+'0';
        else ans+=a[i]-10+'a';
    }
    return ans;
}

int main()
{
    string s1,s2;
    while(cin>>s1>>s2)
    {
        printf("%s\n",add(s1,s2).c_str());
    }
    return 0;
}
           

Parencodings(模拟)

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Let S = s1 s2 ... s2n be a well-formed string of parentheses. S can be encoded in two different ways:

  • By an integer sequence P = p1 p2 ... pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence).
  • By an integer sequence W = w1 w2 ... wn where for each right parenthesis, say a in S, we associate an integer which is the number of right parentheses counting from the matched left parenthesis of a up to a. (W-sequence).

Following is an example of the above encodings:

S (((()()())))

P-sequence 4 5 6666

W-sequence 1 1 1456

Write a program to convert P-sequence of a well-formed string to the W-sequence of the same string.

Input

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case is an integer n (1 <= n <= 20), and the second line is the P-sequence of a well-formed string. It contains n positive integers, separated with blanks, representing the P-sequence.

Output

The output consists of exactly t lines corresponding to test cases. For each test case, the output line should contain n integers describing the W-sequence of the string corresponding to its given P-sequence.

Sample Input

2

6

4 5 6 6 6 6

9

4 6 6 6 6 8 9 9 9

Sample Output

1 1 1 4 5 6

1 1 2 4 5 1 1 3 9

【分析】P序列:第i個右括号前左括号的數量 ;W序列:第i個右括号前成功比對的左括号的數量;題中紅字部分說明括号是成對比對好的,不存在前面有很多個左括号然後最後隻有一個右括号。是以,可以先把這個序列還原,再做。有點麻煩。

【代碼】

#include<bits/stdc++.h>
using namespace std;
char s[100];
int a[100];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		a[0]=0;
		for(int i=1;i<=n;i++)
			scanf("%d",&a[i]);
		int len=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=a[i-1];j<a[i];j++)
				s[len++]='(';
			if(a[i]-a[i-1]>=0)s[len++]=')';
		}
		//for(int i=0;i<len;i++)cout<<s[i];
		int ans=0,num,flag=0;
		for(int i=0;i<len;i++)
		{
			if(s[i]==')')
			{
				num=0;
				for(int j=i;j>=0;j--)
				{
					if(s[j]=='(')ans--;
					else if(s[j]==')')ans++,num++;
					if(ans==0)break;
				}
				if(flag)printf(" ");
				flag=1;
				printf("%d",num);
			}
		}
		puts("");
	}
        return 0;
 }